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、チャネルのフロントエンドにリアルタイム レスポンスを送信します。 'my other event' チャネルで送信された、そのクライアントから受信したすべてのメッセージをリッスンしています。news{ hello: 'world' }socket.on

クライアント側:

まず、socket.io JavaScript ソースを組み込み、クライアントを localhost のポート 80 の socket.io サーバーに接続します。次に、クライアントはチャネル「ニュース」でブロードキャストされたメッセージをリッスンし、そのメッセージをログに記録して、メッセージをブロードキャストします。 「my」プロパティが「data」に設定されたオブジェクトを持つサーバーに送信します。

于 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.
});

これがあなたの理解に役立つことを願っています。pub/sub アプリケーションと考えれば、socket.io を使い始めるのは簡単だと思います (最初は、より多くの機能があるため)。

于 2012-05-23T20:09:44.510 に答える