websocket-sharp 如何使用 socketio 服务器并发送“emit"要求 [英] websocket-sharp how to work with socketio server and send "emit" request
问题描述
我可以连接到我的 socketio 服务器,但是我如何发送带有参数的请求?
i can connect to my socketio server, but how can i send a requst with params?
我的 socketio 服务器监听事件
My socketio server listen event
socket.on('init', ( data ) => {
console.log('init', data);
});
在客户端我尝试这样做
_socket.OnMessage += (sender, e) =>
{
Console.WriteLine("message: {0} {1}", e.IsBinary, e.Data);
_socket.Send(MakePacket("init", new Init
{
key = "1",
room = "eq",
type = "s"
}.ToJson())
);
};
private string MakePacket(string e, string data)
{
return new[] {e, data}.ToJson();
}
所以我将json发送到服务器["init", {"type":"s","key":"1","room":"eq"}]
so i send json to server ["init", {"type":"s","key":"1","room":"eq"}]
但是服务器不会对这个数据包做出反应.服务器工作正常,我只有在 C# 中调用此事件时遇到问题.我做错了什么?
But server wont react at this packet. Server working fine, i have problem only with call this event at C#. What i do wrong?
推荐答案
C# 有 socketio 库,但我有一些问题无法找到答案,而且根本没有支持.所以我切换到 websocket-sharp.
C# has socketio library but i have some issues that i can't find answers at and there are no support at all. So i switched to websocket-sharp.
从 socketio 服务器对调试信息进行一些研究后,我找到了答案.如果我发送这个字符串一切正常
Afer some reseach of debug info from socketio server i found the answer. If i send this string all works fine
42["init", {"type":"s","key":"1","room":"eq"}]
只需在 json 之前添加 42,一切都会正常工作.魔法.我认为这个 42 数字就像您当前的连接状态或类似的东西.因为当您刚刚连接到 socketio 服务器时,它会在 json 之前发送带有 0 的字符串.
Just add 42 before json and all will work fine. Magic. I think this 42 number is like your current connect status or something like this. Because when you just connect to socketio server it's send string with 0 before json.
这篇关于websocket-sharp 如何使用 socketio 服务器并发送“emit"要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!