简体   繁体   English

socket.io如何从客户端获取参数?

[英]How does socket.io get arguments from the client?

I have the following code on the server side: 我在服务器端有以下代码:

var io = socket(server);

io.on('connection', function(socket){
    console.log('made socket connection with ID:', socket.id);
});

Here is what I understand. 这是我的理解。

We make a socket on the server side and pass that (instance?) to io. 我们在服务器端创建一个套接字,并将其(实例?)传递给io。 Then, we say io.on('connection') which is saying listen to the connection event. 然后,我们说io.on('connection'),它说监听连接事件。 We also make a callback function to which we are passing socket. 我们还向回调函数传递了套接字。

I don't understand the callback function(socket) part. 我不了解回调函数(套接字)部分。 Where are we passing it socket? 我们将套接字传递到哪里? on the client side I have this 在客户端,我有这个

var socket = io.connect('http://localhost:4000');

I get that a connection will be made and it will start listening but how and where are we calling the function form the client side? 我知道将建立连接并且它将开始侦听,但是我们如何以及在哪里从客户端调用该函数?

We are not calling the callback, and so we are not passing it anything. 我们没有调用回调,因此也没有传递任何东西。 You can't call a serverside function from the clientside. 您不能从客户端调用服务器端函数。

The callback is called (and given the argument) by the JavaScript's event loop, as triggered by socket.io library's internals - more specifically, as far as I can see, in Engine.io : 回调由JavaScript的事件循环调用(并给定参数),该事件由socket.io库的内部触发-更具体地,据我所知 ,在Engine.io中

Server.prototype.handshake = function (transportName, req) {
  ...
  self.emit('connection', socket);
  ...
};

This is done through Node's native EventEmitter , which allows you to register a callback for an event ( io.on('connection', function(socket) { ... }) ), and emit the event with data for the callback ( self.emit('connection', socket) ). 这是通过Node的本机EventEmitter ,它允许您注册事件的回调io.on('connection', function(socket) { ... }) ),并使用回调的数据发出事件self.emit('connection', socket) )。 The data passed with the event emission is the data received by any callback registered for the event on the emitter object. 与事件发射一起传递的数据是在发射器对象上为该事件注册的任何回调接收的数据。

Thus, the flow is roughly as follows: 因此,流程大致如下:

  • Serverside, you register a callback handler for the connection event on the Engine.io object (passed to you by Socket.io's socket function). 在服务器端,您为Engine.io对象上的connection事件注册了一个回调处理程序(由Socket.io的socket函数传递给您)。
  • Clientside, you invoke the connect function. 在客户端,您调用connect函数。 It starts a Websocket request. 它启动一个Websocket请求。
  • Serverside, Engine.io receives the Websocket request. 在服务器端,Engine.io接收Websocket请求。 It creates a socket object to represent the connection with that particular client, then emits the connection event, with the socket object as accompanying data. 它创建一个socket对象来表示与该特定客户端的connection ,然后发出connection事件,并将socket对象作为随附数据。
  • The connection event causes all handlers registered for that event to be invoked, and passed the accompanying data (the socket object), your function being among them. connection事件将导致为该事件注册的所有处理程序均被调用,并传递附带的数据( socket对象),您的函数就在其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM