node.js - 使用 socket.io 连接 Node.js 和 Python
问题描述
简短的
我正在尝试连接 Node.js [客户端] 和 Python [服务器]。Python 服务器仅支持 WebSocket,其中 Node.js socket.io 正在尝试使用长轮询设置传输:[websocket]。在所有设置之后,我没有得到任何数据,并且 node.js socket.io 一直在尝试重新连接到服务器.
要求
Python 和 Node.js 通过 websocket. 相互连接,其中 Node.js 是客户端,Python 是服务器端。
代码
Python
# IMPORT'S import asyncio # core: python async-await library. import string # core: python string manipulatation library. import json # core: json library. import pydash # pip: utility module. from starlette.applications import Starlette # pip: http framework. from starlette.responses import JSONResponse # pip: http response handler. from starlette.websockets import WebSocket # pip: websocket library. import uvicorn # pip: server runner. # SYSTEM IMPORT'S from system import Connect # system: load engine connection. from www.template import Template # system: route template loader # ENGINE: STARLETTE App = Starlette() App.debug = True # GLOBAL'S _port = 8000 _host = '0.0.0.0' # HANDLER: APP """ Details: Allow's external connection to interact with system core.rightnow socket connection can be made. """ class App: def __init__(self, scope): assert scope['type'] == 'websocket' self.scope = scope async def __call__(self, receive, send): # load and accept socket.io connection. _WebSocket = WebSocket(self.scope, receive=receive, send=send) await _WebSocket.accept() # local variable's. _templateName = _WebSocket.query_params['name'] # only proceed if templateName is defined. if _templateName: # send json response to client. await _WebSocket.send_json({ 'templateReply': 'RecivedData' }, mode= 'binary') # close socket connection. await _WebSocket.close() # return none. as application is # run on socket. return None # SERVER RUN if __name__ == '__main__': # run server. uvicorn.run(App, host= _host, port = _port)
节点.js
// if _user information found than do login verification. else do // refresh @Cr for user information. // if _BroadCast contains data set. let _io = IO.connect('http://localhost:8000', { 'path': '/chat', 'transports': ['websocket'], 'query': { 'name': 'monk', 'POST': 'dotsinspace@gmail.com' } }) _io.on('connection', async () => { console.log('connected to user....') // log: nothing }) _io.on('reconnecting', (__connection) => { console.log(__connection) // log: 2, 3 etc.... })
结果 套接字响应
解决方案
推荐阅读
- javascript - 如何使用 React 中的函数以连续方式选择颜色?
- android - cordova-plugin-admob-free 在构建过程中产生错误?
- algorithm - 给定两棵 B 树,T1 和 T2,使得 T2 中的每个元素都大于 T1 中的每个元素,加入到 B 树中
- google-colaboratory - 你如何在 Google Colab 中缩进?
- java - Java 登录系统:在登录系统中创建新用户
- c# - Settings.Designer 中的 CultureInfo
- swift - 改变 UIImage 的方向?迅速
- java - 如何在 Java 中删除 ArrayList 中的一个重复项
- c++ - 如何从基类返回模板派生类的成员?
- php - PhpStorm 类列表