简体   繁体   English

node.js socket.io-client + python socketio

[英]node.js socket.io-client + python socketio

I am trying to use Socket.IO to allow my Node.JS app to communicate with a Python Backend. 我正在尝试使用Socket.IO来允许我的Node.JS应用程序与Python后端通信。 I want Node.JS to act as the client and Python to act as the server, so I am using the socket.io-client Node.JS module in addition to the gevent-socketio python module. 我希望Node.JS充当客户端,Python充当服务器,所以除了gevent-socketio python模块之外,我还使用了socket.io-client Node.JS模块。

https://github.com/LearnBoost/socket.io-client https://github.com/abourget/gevent-socketio https://github.com/LearnBoost/socket.io-client https://github.com/abourget/gevent-socketio

Here is my python server: 这是我的python服务器:

#!/usr/bin/env python
from socketio.server import SocketIOServer
from socketio.namespace import BaseNamespace

class MyNamespace(BaseNamespace):
    def on_foobar(self,data):
        print 'received method for foobar'
        print data


server = SocketIOServer(('localhost', 1234),resource=MyNamespace,policy_server=False)
print 'SocketIO server listening...'
server.serve_forever()

Here is my Node.JS server (acting as client): 这是我的Node.JS服务器(充当客户端):

#!/usr/bin/env node
var io = require('socket.io-client');
var PySocket = io.connect('localhost:1234');
PySocket.emit('foobar',{'key1':'value1'});

For some reason, the python server is not seeing the connection. 出于某种原因,python服务器无法看到连接。 can anyone point out what I am missing? 谁能指出我所缺少的吗?

You'll need a little bit more on the Python side. 您将需要在Python方面多一点。

The Namespace object is not to be passed as a parameter to the SocketIOServer object. 命名空间对象不应作为参数传递给SocketIOServer对象。 That resource (later renamed to namespace ) is only the name of the path to be recognized (like http://localhost/[namespace]/[rest of the socket.io protocol path]. I agree there is an overlap in terminology, but we rarely deal with a resource/namespace other than socket.io . resource (后来重命名为namespace )只是要识别的路径的名称(例如http:// localhost / [namespace] / [socket.io协议路径的其余部分]。我同意术语存在重叠,但是我们很少处理socket.io以外的资源/命名空间。

Now, for your python IO-server to run, you'll need to wrap it using some framework.. to actually dispatch some incoming request to the correct handler. 现在,要使您的python IO服务器运行,您需要使用某种框架进行包装。以实际将一些传入请求分派到正确的处理程序。 That handler must execute socketio_manage() and this is the function where you should pass your Namespace object as a parameter. 该处理程序必须执行socketio_manage() ,这是您应将Namespace对象作为参数传递的函数。 Also, your framework will probably want to serve other files, like the .swf ... gevent-socketio doesn't do that for you. 另外,您的框架可能会希望提供其他文件,例如.swf ... gevent-socketio不会为您执行此操作。 Also, if you want your python process to do anything (like interact with databases, load some configuration files), I recommend you pick a framework, as it will ease your life for mostly anything you'll need to do. 另外,如果您希望python进程做任何事情(例如与数据库交互,加载一些配置文件),我建议您选择一个框架,因为它可以简化您的大部分工作。

If you really just want to have a socket -type of server, from node.js to python, then why not use the standard TCP/UDP sockets ? 如果您真的只想拥有一个socket类型的服务器(从node.js到python),那么为什么不使用标准的TCP / UDP套接字呢? In that case, you wouldn't need the overhead of a framework, the encoding/decoding of the Socket.IO protocol, etc.. 在这种情况下,您将不需要框架的开销,Socket.IO协议的编码/解码等。

What is your particular use case ? 您的特定用例是什么? Maybe this could shed some light on the way to go. 也许这可以为下一步的发展提供一些启示。

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

相关问题 Python Socketio 未与 JavaScript socket.io-client 连接 - Python Socketio not connecting with JavaScript socket.io-client python-socketio 客户端无法获取公共数据,但 NodeJS socket.io-client 没有 - python-socketio client fails to get public data but NodeJS socket.io-client does not 使用 Python flask_socketio 不处理事件来反应 socket.io-client - React socket.io-client with Python flask_socketio not handling events Flask socketio 服务器 + React socket.io-client 连接被拒绝 - Flask socketio server + React socket.io-client connection refused 如何将客户端的 Python 套接字连接到 Node.js/socket.io? - How to connect a Python socket on client-side to Node.js/socket.io? 将 React Native Socket.io-client 与 Python 服务器连接起来 - Connecting React Native Socket.io-client with Python Server 格式化消息以从 python 客户端发送到 socket.io node.js 服务器 - Formatting messages to send to socket.io node.js server from python client Python socket.io 客户端在 Heroku 上托管后无法向服务器(node.js)发出事件 - Python socket.io client unable to emit event to server(node.js) after hosting on Heroku 通过socket.io将python对象发送到node.js - Sending python objects to node.js via socket.io 将 Python socketio 服务器连接到 node js socketio 客户端时遇到问题 - Trouble connecting Python socketio server to node js socketio client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM