Socketio - flask服务器和c#客户端

时间:2016-01-20 09:37:36

标签: c# python flask flask-socketio

嘿,我正在尝试使用SocketIO创建一个消息传递应用程序。我有一个用烧瓶写的服务器和一个用C#编写的客户端。我使用SocketIoClientDotNet作为客户端(https://github.com/Quobject/SocketIoClientDotNet)。

这是我的代码:

Flask: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后,我开始接收连接事件。问题是它停留在它上面而没有调用其他函数。看着烧瓶控制台看起来它使用了pull而不是webSocket(某种后卫机制)所以我​​试图改变c#socket选项:

    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); 

但它保持打印传输=轮询

Edit2:

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

2 个答案:

答案 0 :(得分: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)
});

答案 1 :(得分:0)

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