嘿我試圖創建一個使用SocketIO的消息應用程序。我有一個用燒瓶寫的服務器和用C#編寫的客戶端。我爲客戶使用SocketIoClientDotNet
(https://github.com/Quobject/SocketIoClientDotNet)。Socketio - 燒瓶服務器和C#客戶端
這是我的代碼:
瓶: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);
,但會將打印傳送=輪詢
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
要使WebSocket可用,您必須安裝gevent或eventlet,並且客戶端應支持升級,以使該部分看起來沒問題。請通過將服務器初始化爲'socketio = SocketIO(app,logger = True,engineio_logger = True)'將日誌記錄添加到服務器。然後請爲您的問題添加一個日誌會話。 – Miguel
用記錄器信息編輯 –
客戶端反覆嘗試連接服務器。由於某種原因,它不斷髮送連接請求。在粘貼的短日誌中有5個不同的連接,您可以通過查看'sid'值(十六進制字符的長序列)來區分它們。我建議您使用官方的Javascript客戶端測試相同的服務器,並比較服務器日誌的輸出。然後,您可能需要將這兩個日誌發送給C#客戶端的開發人員。 – Miguel