1

我正在探索 socket.io 并试图了解客户端和服务器端之间基本交互的细节。有人可以给我下面这个基本 socket.io 程序的具体评论和解释。感谢您的帮助,当然,如果您给我一个好的答案,我会给您打分!

服务器端

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

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

客户端

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>
4

2 回答 2

2

服务器端:

第一行首先在端口 80 上绑定 socket.io。

然后为每个连接编写处理程序io.sockets.on('connection', function (socket) {。在该处理程序socket中包含有关已连接客户端的信息。使用 JSON 对象socket.emit向通道上的前端发送实时响应。 正在侦听从该客户端接收到的在“我的其他事件”通道上传输的所有消息。news{ hello: 'world' }socket.on

客户端:

您首先包含 socket.io JavaScript 源并将客户端连接到 localhost 端口 80 上的 socket.io 服务器。然后客户端侦听在频道“新闻”上广播的消息,然后记录该消息并广播回一条消息将“我的”属性设置为“数据”的对象发送到服务器。

于 2012-05-23T14:20:20.080 回答
1

这是我帮助您弄湿脚的最佳方法...

首先是第一件事..必须设置您的服务器端文件...

var http = require('http');
var app = http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end();
});

var io = require('socket.io').listen(app);
io.set('log level', 1); // keep logging to only the essentials
io.set('transports', ['websocket']); // this will only allow clients that support web sockets


io.sockets.on('connection', function(socket) { // when a socket connects and publishes a "connection" message Think of this like pub/sub here.. The connection event happens any time a client connects.
    socket.on('GetSession', function(){
        socket.emit('SessionID', {
            SessionID : socket.id //socket.id = the socket ID that is auto generated. So what we are doing here is when a client connects we will send back to them their session ID. I use this as a simple way to verify that the connection is successful on the client side.
        });
    socket.on('SendMessage', function(msg){ // So here is an example of that pub/sub I am talking about. We are going to publish to anything listening on "CreateRoom" a message.
        socket.emit('Message', msg); // Here we take anyone listening on the message channel and send them whatever was emitted.
    });
});

app.listen(3000);

接下来让我们看看客户...

var socket = io.connect('http://localhost:3000');
function GetInfo(){
    socket.emit('GetSession', ''); // The client is now initiating the request we setup earlier to get its socket.id from the server.
}
socket.on('SessionID', function(msg) {
    socket.emit('SendMessage', msg + ' Has Connected'); // When the server returns the socket.id the client will emit a message to the server for anyone else who is subscribed to the "message" channel. My terminology for these explanations is not accurate but I think that this way of stating things is a lot easier to wrap your head around...
});
    socket.on('message', function(msg) {
    console.log(msg); // here the client is listening on the "message" channel. Whenever the server sends a message to this channel the client will write the data to the console.
});

希望这可以帮助你看到。如果您将 socket.io 视为一个发布/订阅应用程序,我认为它很容易上手(起初,因为有更多功能)。

于 2012-05-23T20:09:44.510 回答