当我使用python-socketio和gunicorn在python环境中开发一些socket.io服务时,我在这里遇到了一个问题。
我使用的是 Mac OS X,我使用的是 python 3.7。
环境设置
$ pip install python-socketio
$ pip install gunicorn
服务器端代码
app.py
import socketio
sio = socketio.Server()
app = socketio.WSGIApp(sio, static_files = {
'/': './socketio-client.html'
})
@sio.event
def connect(sid, environ):
print(sid, 'connected')
@sio.event
def disconnect(sid):
print(sid, 'disconnected')
Run Code Online (Sandbox Code Playgroud)
客户端代码
socketio-client.html
<html>
<head>
<title>Socket.IO Demo</title>
<script src="https://cdn.socket.io/3.1.3/socket.io.min.js" integrity="sha384-cPwlPLvBTa3sKAgddT6krw0cJat7egBga3DJepJyrLl4Q9/5WLra3rrnMcyTyOnh" crossorigin="anonymous"></script>
</head>
<body>
<h1>Socket.IO Demo</h1>
<script>
const sio = io();
sio.on('connect', () => {
console.log('connected');
});
sio.on('disconnect', () => {
console.log('disconnected');
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
启动程序
我将文件放在同一文件夹中。并按照命令启动它。
$ …