Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有人能对socket.io给出具体的评论和解释吗;“helloworld”;相等的_Javascript_Html_Node.js_Socket.io - Fatal编程技术网

Javascript 有人能对socket.io给出具体的评论和解释吗;“helloworld”;相等的

Javascript 有人能对socket.io给出具体的评论和解释吗;“helloworld”;相等的,javascript,html,node.js,socket.io,Javascript,Html,Node.js,Socket.io,我正在探索socket.io,并试图了解客户端和服务器端之间基本交互的细节。有人能给我具体的评论和解释这个基本的socket.io程序下面。我感谢你的帮助,当然,如果你给我一个好的答案,我会给你评分 服务器端 var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on

我正在探索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>
客户端

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>

var socket=io.connect('http://localhost');
socket.on('news',函数(数据){
控制台日志(数据);
emit('my other event',{my:'data'});
});
服务器端:

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

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

客户端:

首先包括socket.io JavaScript源代码,并将客户端连接到本地主机端口80上的socket.io服务器。
然后,客户端侦听“新闻”频道上广播的消息,然后记录该消息,并使用“我的”属性设置为“数据”的对象将消息广播回服务器。

以下是我帮助您湿脚的最佳方法

第一件事是第一件事。必须设置服务器端文件…

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.
});
接下来让我们看一看客户机…

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视为一个发布/订阅应用程序,我认为它很容易入门(首先,因为它有很多功能)。

很抱歉客户端的格式很奇怪