嘿,我正在尝试使用 SocketIO 创建一个消息传递应用程序。我有一个用烧瓶编写的服务器和一个用 C# 编写的客户端。我正在使用SocketIoClientDotNet
客户端(https://github.com/Quobject/SocketIoClientDotNet)。
这是我的代码:
烧瓶:routes.py
from flask import Flask
from flask.ext.socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@app.route('/')
def home():
return "hello"
@socketio.on('connect')
def connect():
print("CONNECTED")
@socketio.on("message")
def test():
print("TEST WORKS")
if __name__ == '__main__':
socketio.run(app)
C# :
public static class EnvConnections {
public static IO.Options CreateOptions() {
var options = new IO.Options();
options.Port = 5000;
options.Hostname = "127.0.0.1";
options.ForceNew = true;
return options;
}
public static void Connect() {
Uri uri = new Uri("http://127.0.0.1:5000");
var socket = IO.Socket(uri, CreateOptions()); //also tried to create the socket only with url IO.Socket("http://127.0.0.1:5000")
socket.On(Socket.EVENT_CONNECT, () => {
Console.Write("EVENT_CONNECT");
socket.Disconnect();
});
socket.Connect(); //tried to remove this
Console.ReadLine();
}
在这两个平台上都没有打印任何内容。看起来它们之间建立了连接(在烧瓶上,一旦建立连接,这条线就会无限打印127.0.0.1 - - [20/Jan/2016 11:23:43] "GET /socket.io/?EIO=3&transport=polling&t=635888858234059292-4049&b64=1 HTTP/1.1" 200 -
:)
我究竟做错了什么?
编辑:安装后gevent
我开始接收连接事件。问题是它卡在上面并且没有调用其他函数。看着烧瓶控制台,它看起来像是使用拉动而不是 webSocket(某种后卫机制),所以我尝试更改 c# 套接字选项:
IO.Options options = new IO.Options();
//options.Transports = ImmutableList.Create<string>(Polling.NAME);
options.Upgrade = true;
var socket = IO.Socket("http://127.0.0.1:5000", options);
但它一直在打印传输=轮询
编辑2:
fb0f4c189334468dba90255880b528a4: Sending packet MESSAGE with 0
127.0.0.1 - - [20/Jan/2016 17:49:13] "GET /socket.io/?EIO=3&transport=polling&t=635889089536079075-2035&b64=1 HTTP/1.1" 200 355 0.002000
5cea6ce4dd8b47d79e79dd96747c26e4: Sending packet OPEN with {'pingTimeout': 60000, 'sid': '5cea6ce4dd8b47d79e79dd96747c26e4', 'upgrades': ['websocket'], 'pingInterval': 25000}
5cea6ce4dd8b47d79e79dd96747c26e4: Sending packet MESSAGE with 0
127.0.0.1 - - [20/Jan/2016 17:49:13] "GET /socket.io/?EIO=3&transport=polling&t=635889089536109077-2036&b64=1 HTTP/1.1" 200 355 0.003000
f768281808204af995421b4dbb2cda1c: Sending packet OPEN with {'pingTimeout': 60000, 'sid': 'f768281808204af995421b4dbb2cda1c', 'upgrades': ['websocket'], 'pingInterval': 25000}
CONNECTED
CONNECTED
f768281808204af995421b4dbb2cda1c: Sending packet MESSAGE with 0
127.0.0.1 - - [20/Jan/2016 17:49:13] "GET /socket.io/?EIO=3&transport=polling&t=635889089536149079-2037&b64=1 HTTP/1.1" 200 355 0.002000
e4eb16fcd12f41898ba58a309014b85c: Sending packet OPEN with {'pingTimeout': 60000, 'sid': 'e4eb16fcd12f41898ba58a309014b85c', 'upgrades': ['websocket'], 'pingInterval': 25000}
e4eb16fcd12f41898ba58a309014b85c: Sending packet MESSAGE with 0
127.0.0.1 - - [20/Jan/2016 17:49:13] "GET /socket.io/?EIO=3&transport=polling&t=635889089536179081-2038&b64=1 HTTP/1.1" 200 355 0.002000
70f45826d89b42bba3842e41b8ae8b08: Sending packet OPEN with {'pingTimeout': 60000, 'sid': '70f45826d89b42bba3842e41b8ae8b08', 'upgrades': ['websocket'], 'pingInterval': 25000}
70f45826d89b42bba3842e41b8ae8b08: Sending packet MESSAGE with 0
127.0.0.1 - - [20/Jan/2016 17:49:13] "GET /socket.io/?EIO=3&transport=polling&t=635889089536219083-2039&b64=1 HTTP/1.1" 200 355 0.002000
CONNECTED
CONNECTED