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

查看:229
本文介绍了node.js socket.io-client + python socketio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Socket.IO来允许我的Node.JS应用与Python后端进行通信.我希望Node.JS充当客户端,Python充当服务器,所以除了gevent-socketio python模块之外,我还使用了socket.io-client Node.JS模块.

I am trying to use Socket.IO to allow my Node.JS app to communicate with a Python Backend. 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.

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

这是我的python服务器:

Here is my python server:

#!/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()

这是我的Node.JS服务器(充当客户端):

Here is my Node.JS server (acting as client):

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

由于某种原因,python服务器没有看到连接.有人可以指出我所缺少的吗?

For some reason, the python server is not seeing the connection. can anyone point out what I am missing?

推荐答案

在Python方面,您还需要更多一点.

You'll need a little bit more on the Python side.

不能将Namespace对象作为参数传递给SocketIOServer对象.该resource(后来重命名为namespace)只是要识别的路径的名称(例如http://localhost/[namespace]/[socket.io协议路径的其余部分].我同意有一个术语上有重叠,但是除了socket.io以外,我们很少处理其他资源/名称空间.

The Namespace object is not to be passed as a parameter to the SocketIOServer object. 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.

现在,要使您的python IO服务器运行,您需要使用某种框架进行包装.以实际将一些传入请求分派到正确的处理程序.该处理程序必须执行socketio_manage(),这是您应将Namespace对象作为参数传递的函数.另外,您的框架可能会希望提供其他文件,例如.swf ... gevent-socketio不会为您执行此操作.另外,如果您希望python进程做任何事情(例如与数据库交互,加载一些配置文件),我建议您选择一个框架,因为它可以简化您的大部分工作.

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. That handler must execute socketio_manage() and this is the function where you should pass your Namespace object as a parameter. Also, your framework will probably want to serve other files, like the .swf ... gevent-socketio doesn't do that for you. 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.

如果您真的只想拥有socket类型的服务器(从node.js到python),那么为什么不使用标准的TCP/UDP套接字呢?在这种情况下,您将不需要框架的开销,Socket.IO协议的编码/解码等.

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 ? In that case, you wouldn't need the overhead of a framework, the encoding/decoding of the Socket.IO protocol, etc..

您的特定用例是什么?也许这可以为下一步的发展提供一些启示.

What is your particular use case ? Maybe this could shed some light on the way to go.

这篇关于node.js socket.io-client + python socketio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆