【发布时间】:2018-02-04 09:53:01
【问题描述】:
我正在尝试通过 websockets 将数据从 python 发送到 nodejs。
Js:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
server.listen(3000, () => console.log('running on 3000'));
io.sockets.on('connection', function(socket) {
io.sockets.on('msg', function(data) {
console.log(data);
});
});
Python:
import asyncio
import websockets
async def sendData(uri, data):
// FAILS ON THE FOLLOWING LINE:
async with websockets.connect(uri) as websocket:
await websocket.send(json.dumps(data))
def main():
text = "sample data"
asyncio.get_event_loop().run_until_complete(
sendData("ws://localhost:3000", {"msg": text})
)
main()
我收到“格式错误的 HTTP 消息”错误 (websockets.exceptions.InvalidMessage: Malformed HTTP message)
我担心 python websockets 可能无法与 socket.io 交互。想法?
【问题讨论】: