2

All:

I am new to node.js and socket.io, and have no idea about how the protocol works. But I am learning.

I got a beginner question about socket.io,if I got code from some tutorial like:

var http = require('http');
var fs = require('fs');

var app = http.createServer(
        function(request, response){
            console.log("New Connection!");
            fs.readFile(
                "client.html","utf-8", 
                function(err, data){
                    response.writeHead(200, {'Content-Type':'text/html'});
                    response.write(data);
                    response.end();
                }
            );
        }
).listen(1337);



var io = require('socket.io').listen(app);

io.sockets.on(
            'connection',
            function( socket ){
                socket.on(
                        'message_to_server',
                        function(data){
                        io.sockets.emit('message_to_client',{message: data['message']});
                        }
                );
            }
);

My question is can someone tells me how this code works? What I am confused is:

It seems like the socket.io just wraps the app,sending and receiving event message and doing callback. But I wonder why those action will not triger app's request event, I put console.log("New Connection!"); inside that callback, and it only displays when I input the server address and press enter in browser, but when I press the send button, it will not display anything?

Here is my client code:

<!DOCTYPE html>

<html>
    <head>
        <script src="/socket.io/socket.io.js"></script>
        <script type="text/javascript">
            // Out client code here.
            var socketio = io.connect("192.168.1.150:1337");
            socketio.on(
                    'message_to_client',
                    function(data){
                        document.getElementById("chatlog")
                        .innerHTML=
                        ("<hr/>"+data['message']+document.getElementById("chatlog").innerHTML);
                    }
            );

            function sendMessage(){
                var msg = document.getElementById("message_input").value;
                if(msg!=""){
                    socketio.emit("message_to_server", {message:msg});
                }
            }
        </script>
    </head>
    <body>
        <input type="text" id="message_input" />
        <button onclick="sendMessage()">SEND</button>
        <div id="chatlog"></div>
    </body>
</html>
4

1 回答 1

4

在您的代码app中是一个 http 服务器,并且io是一个 websocket 服务器。如您所知,http 服务器服务于 http 请求,而 websocket 服务器使用 websocket 协议。检查维基

  • http:// 是 http 协议, https:// 是 http 安全协议
  • ws:// 是 web socket 协议, wss:// 是 web socket 安全协议

这是做什么的

var app = http.createServer(
    function(request, response){
        console.log("New Connection!");

它启动一个 http 服务器并在每个 http 请求上执行给定的函数。对您来说,它会打印New Connection并发送 client.html。

这是做什么的

var io = require('socket.io').listen(app);

它告诉套接字服务器挂钩到 http 服务器。它在与 http/https 服务器相同的地址和端口上侦听 websocket 连接。

这是做什么的

var socketio = io.connect("192.168.1.150:1337");

在 192.168.1.150:1337 连接到 websocket 服务器并使用套接字进行通信。您说单击发送时没有任何显示,您使用的地址是否正确?试试这个,它会自动获取地址(正在使用):

var socketio = io.connect("");
于 2013-04-12T17:27:05.223 回答