简体   繁体   English

Node.js的非事件驱动套接字模块

[英]Non-event-driven socket module for Node.js

I've created a server with Node.js using Socket.io and now I need to let a Python client connect to it. 我已经使用Socket.io使用Node.js创建了服务器,现在需要让Python客户端连接到它。

What troubles me is to understand how the "keywords" in Socket.io affects this connection. 困扰我的是要了解Socket.io中的“关键字”如何影响此连接。 For example, this is how it is originally done on the server side: 例如,这是最初在服务器端完成的方式:

io.on('connection', function(socket){

  socket.on('request', function(msg){
     console.log(msg);
  });
});

which means that the Python client would have to do something like this (if I were to run it as a script in the terminal): 这意味着Python客户端将必须执行以下操作(如果我要在终端中将其作为脚本运行):

establishConnection 12.12.12.12:3000 'connection'
//connection established ...
sendData('request', {data : 'hello'});

This is not preferable, and I need to modify the server to accept socket requests without the keywords, like this 这不是可取的,我需要修改服务器以接受没有关键字的套接字请求,像这样

establishConnection 12.12.12.12:3000
//connection established ...
sendData({data : 'hello'});

Is this possible to do with Socket.io? 这可能与Socket.io有关吗?

There is no way, in the api, to get all packages from the socket.io library. 在api中,无法从socket.io库获取所有软件包。

To get all packets from socket.io you can do an ugly hack and override onevent or onpacket of the socket . 要从socket.io获取所有数据包,您可以进行丑陋的修改,并覆盖socket oneventonpacket

Like this: 像这样:

socket.onevent = function () {
    console.log(arguments);
}

{ '0': { type: 2, nsp: '/', data: [ 'chat message', 'hei' ] } } {'0':{类型:2,nsp:'/',数据:['聊天消息','嘿']}}

Note that this is undocumented, and unsupported, so the API might very likely break. 请注意,这是未记录且不受支持的,因此该API很可能会中断。

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

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