1

嘿,我正在尝试使用 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
4

2 回答 2

0

要将我的评论变成正确的答案:

问题似乎是从长轮询到 WebSockets 的升级机制在这些特定的 socket.io 实现之间不起作用。通过将客户端配置为直接使用 WebSockets,我得到了它的工作。这里有一个例子:

var socket = IO.Socket("http://127.0.0.1:5000/data", new IO.Options
{
    // The protocol upgrading does not properly work between Flask-SocketIO and SocketIOClientDotNet
    Transports = ImmutableList<string>.Empty.Add(WebSocket.NAME)
});
于 2016-12-04T20:08:25.560 回答
0

问题与服务器发回的编码有关。Flask Socket IO 不返回 text/plain,这是当请求中包含 base64 参数时返回的内容(b64=1)。相反,它返回一个八位字节流。C# 库会自动将 b64=1 添加到请求中,不幸的是,更改 ForceBase64 属性不会改变这一点。目前,我发现解决此问题的唯一方法是调整 EngineIoClientDotNet 的源,编辑 Polling.cs 并删除 query.Add("b64","1")。如果库不支持二进制但到目前为止它现在连接成功,这可能会产生不良影响。

于 2017-01-09T15:44:56.387 回答