PHP套接字服务器与node.js:网络聊天 [英] PHP Socket Server vs node.js: Web Chat

查看:81
本文介绍了PHP套接字服务器与node.js:网络聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用长期存在的HTTP请求(Comet),ajax和websocket(取决于所使用的浏览器)对HTTP WebChat进行编程。 Userdatabase在mysql中。聊天用PHP编写,但聊天流本身也可以用javascript(node.js)编写:

I want to program a HTTP WebChat using long-held HTTP requests (Comet), ajax and websockets (depending on the browser used). Userdatabase is in mysql. Chat is written in PHP except maybe the chat stream itself which could also be written in javascript (node.js):

我不想每个人都启动php进程用户,因为没有很好的方法可以在这些php子对象之间发送聊天消息。
因此,我想到了用PHP或node.js编写自己的套接字服务器,该服务器应该能够处理超过1000个连接(聊天用户)。作为纯Web开发人员(php),我对套接字不太熟悉,因为我通常让Web服务器关心连接。聊天消息不会保存在磁盘上或mysql中,而是以数组或对象的形式以最佳速度保存在RAM中。

I don't want to start a php process per user as there is no good way to send the chat messages between these php childs. So I thought about writing an own socket server in either PHP or node.js which should be able to handle more then 1000 connections (chat users). As a purely web developer (php) I'm not much familiar with sockets as I usually let web server care about connections. The chat messages won't be saved on disk nor in mysql but in RAM as an array or object for best speed.

据我所知没有办法在单个php进程(套接字服务器)中同时处理多个连接,但是您可以接受大量的套接字连接,并在循环中依次处理它们(读和写;传入消息->写入所有套接字连接)。问题是〜1000个用户最有可能会出现滞后,而mysql操作可能会减慢整个过程的速度,进而影响所有用户。

As far as I know there is no way to handle multiple connections at the same time in a single php process (socket server), however you can accept a great amount of socket connections and process them successive in a loop (read and write; incoming message -> write to all socket connections). The problem is that there will most-likely be a lag with ~1000 users and mysql operations could slow the whole thing down which will then affect all users.

我的问题是:node.js可以处理性能更好的套接字服务器吗? Node.js是基于事件的,但是我不确定它是否可以同时处理多个事件(是否需要多线程?)还是只有一个事件队列。有了事件队列,就像php:在用户之后处理用户。

My question is: Can node.js handle a socket server with better performance? Node.js is event-based but I'm not sure if it can process multiple events at the same time (wouldn't that need multi-threading?) or if there is just an event queue. With an event queue it would be just like php: process user after user.

我还可以在每个聊天室中生成一个php进程(用户数量少得多),但是afaik有单线程IRC服务器也可以处理成千上万的用户。 (用C ++或其他语言编写),所以也许也可以在php中使用。

I could also spawn a php process per chat room (much less users) but afaik there are singlethreaded IRC servers which are also capable to handle thousands of users. (written in c++ or whatever) so maybe it's also possible in php.

我更喜欢PHP而不是Node.js,因为那样的话,该项目将仅是php,而不是混合编程语言。但是,如果Node可以同时处理连接,我可能会选择它。

I would prefer PHP over Node.js because then the project would be php-only and not a mixture of programming languages. However if Node can process connections simultaneously I'd probably choose it.

推荐答案

JavaScript,或者在这种情况下为引擎V8 Node正在使用的是通过 design 设计的单线程。所以是的,这里只有一个事件队列。

JavaScript, or in this case V8 which is the engine that Node is using, is by design single threaded. So yes there's just an event queue.

但是最后,这不是问题,总是会首先发生,除非您使用多个处理器,即使这样,您极有可能只有一张网卡...一张路由器...您就会明白。另外,使用1000多个线程...不是一个好主意,扩展性很差,您会发现自己处于并发 Hell

But in the end, that's not a problem, something's always gonna happen first, unless you're using multiple processors, and even then, you will most likely only have one network card... one router... you get the idea. Also, using 1000+ threads... not a good idea, scales badly, and you will find yourself in a concurrency HELL.

1000聊天用户,对于Node.js来说根本没有问题。

1000 chat users, that will be no problem at all for Node.js.

我可以给你一个基本的 >知道如何设置,这种普通的香草聊天方式在telnet上很有效,它没有任何功能,但是可以正常工作:

I can give you a pretty basic idea how you would set it up, this plain vanilla chat thingy works over telnet, it has.. no features, but it works:

var net = require('net'); // require the net module

var users = []; // keep track of the users

// setup a new tcp socket server
net.createServer(function(socket) { // provide a callback in case a new connection gets
                                    // established, socket is the socket object

    // keep track of this users names, via use of closures
    var name = '';

    // ask the new user for a name
    socket.write('Enter a Name(max 12 chars): ');

    // register a callback on the socket for the case of incoming data
    socket.on('data', function(buffer) { // buffer is a Buffer object containing the data
        if (name !== '') {  // in case this user has a name...

            // send out his message to all the other users...
            for(var i = 0; i < users.length; i++) {
                if (users[i] !== socket) { // ...but himself
                    users[i].write(name + ': '
                                   + buffer.toString('ascii').trim()
                                   + '\r\n');
                }
            }

        // otherwise take the data and use that as a name
        } else {
            name = buffer.toString('ascii').substring(0, 12).trim().replace(/\s/g, '_');
            socket.write('> You have joined as ' + name + '\r\n');

            // push this socket to the user list
            users.push(socket);
            for(var i = 0; i < users.length; i++) {
                if (users[i] !== socket) {
                    users[i].write('> ' + name + ' has joined' + '\r\n');
                }
            }
        }
    });

    // another callback for removing the user aka socket from the list
    socket.on('end', function() {
        users.splice(users.indexOf(socket), 1);
    });

// bind the server to port 8000
}).listen(8000);

这里没有任何魔力(除了使用关闭),您不必进行原始套接字编程,也不会出现任何并发问题。并且您会学到一些最新的热门话题;)

There's no magic involved in here (besides the use of a closures), you don't have to do with raw socket programming and you won't have any concurrency problems. And you learn some of the latest hotness ;)

我建议您观看我们 Node.js 标签Wiki,以更好地了解Node.js的工作原理。

I recommend that you watch some of the talks that are listed on our Node.js tag wiki, to get a better grasp of how Node.js works.

这篇关于PHP套接字服务器与node.js:网络聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆