如何在 python-socketio 中将消息从 python 服务器发送到 javascript 客户端?
How to emit message from python server to javascript client in python-socketio?
socketio 客户端成功连接到服务器并向服务器发送带有emit
的消息,但另一个方向服务器到客户端失败。我找不到错误的来源。这是
这里是 app.py
中的服务器 python,基于 python-socketio 网站中的示例:
from aiohttp import web
import socketio
sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)
async def index(request):
"""Serve the client-side application."""
with open('index.html') as f:
return web.Response(text=f.read(), content_type='text/html')
@sio.on('connect', namespace='/chat')
def connect(sid, environ):
print("connect", sid)
@sio.on('chat message', namespace='/chat')
async def message(sid, data):
print("server received message!", data)
await sio.emit('reply', data)
await sio.send(data)
@sio.on('disconnect', namespace='/chat')
def disconnect(sid):
print('disconnect', sid)
app.router.add_static('/static', 'static')
app.router.add_get('/', index)
if __name__ == '__main__':
web.run_app(app)
我尝试评论 await sio.emit('reply', data)
或 await sio.send(data)
之一,但结果是一样的。这是 index.html
中的 javascript 客户端:
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"></script>
</head>
<body>
<form id="the_form">
<input type="input" name="msg" id="msg"></input>
<input type="submit" value="➤"></input>
</form>
<script>
var socket = io('http://localhost:8080/chat');
socket.on('connect', function(){console.log('connect!')});
socket.on('message', function(msg){console.log('message!', msg)});
socket.on('disconnect', function(){console.log('disconnect!')});
socket.on('reply', function(msg){console.log('reply!', msg)});
document.getElementById('the_form').onsubmit = function(e) {
let msg = document.getElementById('msg').value;
document.getElementById('msg').value = '';
// send it to the server
socket.emit('chat message', msg);
return false
};
</script>
</body>
</html>
在终端window,我运行服务器。然后,我打开两个浏览器 windows(Chrome 版本 69.0.3497.100(正式版)(64 位))并从其中一个浏览器发送 'test'。这是我在每个 window:
上看到的
航站楼
$ python3 app.py
======== Running on http://0.0.0.0:8080 ========
(Press CTRL+C to quit)
connect 9b18034f7b8b4d4c857dec394ef01429
connect 3adea48a3e00459f807855da0337599c
server received message! test
Window 1(控制台日志)
connect!
Window 2(控制台日志)
connect!
根据示例here suggested in ,此处需要命名空间参数。似乎默认命名空间,至少在这个版本上,不是异步函数的命名空间。因此,使用回显消息进行广播的正确方法如下:
@sio.on('chat message', namespace='/chat')
async def message(sid, data):
print("server received message!", data)
await sio.emit('reply', data, namespace='/chat')
socketio 客户端成功连接到服务器并向服务器发送带有emit
的消息,但另一个方向服务器到客户端失败。我找不到错误的来源。这是
这里是 app.py
中的服务器 python,基于 python-socketio 网站中的示例:
from aiohttp import web
import socketio
sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)
async def index(request):
"""Serve the client-side application."""
with open('index.html') as f:
return web.Response(text=f.read(), content_type='text/html')
@sio.on('connect', namespace='/chat')
def connect(sid, environ):
print("connect", sid)
@sio.on('chat message', namespace='/chat')
async def message(sid, data):
print("server received message!", data)
await sio.emit('reply', data)
await sio.send(data)
@sio.on('disconnect', namespace='/chat')
def disconnect(sid):
print('disconnect', sid)
app.router.add_static('/static', 'static')
app.router.add_get('/', index)
if __name__ == '__main__':
web.run_app(app)
我尝试评论 await sio.emit('reply', data)
或 await sio.send(data)
之一,但结果是一样的。这是 index.html
中的 javascript 客户端:
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"></script>
</head>
<body>
<form id="the_form">
<input type="input" name="msg" id="msg"></input>
<input type="submit" value="➤"></input>
</form>
<script>
var socket = io('http://localhost:8080/chat');
socket.on('connect', function(){console.log('connect!')});
socket.on('message', function(msg){console.log('message!', msg)});
socket.on('disconnect', function(){console.log('disconnect!')});
socket.on('reply', function(msg){console.log('reply!', msg)});
document.getElementById('the_form').onsubmit = function(e) {
let msg = document.getElementById('msg').value;
document.getElementById('msg').value = '';
// send it to the server
socket.emit('chat message', msg);
return false
};
</script>
</body>
</html>
在终端window,我运行服务器。然后,我打开两个浏览器 windows(Chrome 版本 69.0.3497.100(正式版)(64 位))并从其中一个浏览器发送 'test'。这是我在每个 window:
上看到的航站楼
$ python3 app.py
======== Running on http://0.0.0.0:8080 ========
(Press CTRL+C to quit)
connect 9b18034f7b8b4d4c857dec394ef01429
connect 3adea48a3e00459f807855da0337599c
server received message! test
Window 1(控制台日志)
connect!
Window 2(控制台日志)
connect!
根据示例here suggested in
@sio.on('chat message', namespace='/chat')
async def message(sid, data):
print("server received message!", data)
await sio.emit('reply', data, namespace='/chat')