C# 在SocketIO4net中未收到任何消息
我试图使用SocketIO4net使用socket.io API,但似乎无法接收任何消息。在相同的VS2010解决方案中,我有一个具有以下代码的网站:C# 在SocketIO4net中未收到任何消息,c#,socket.io,socketio4net,C#,Socket.io,Socketio4net,我试图使用SocketIO4net使用socket.io API,但似乎无法接收任何消息。在相同的VS2010解决方案中,我有一个具有以下代码的网站: <script src="https://api.icbit.se/socket.io/socket.io.js"></script> <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></scri
<script src="https://api.icbit.se/socket.io/socket.io.js"></script>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
<script>
$(document).ready(function () {
var conn = io.connect('http://api-url');
conn.on('connect', function () {
alert('Connected');
conn.emit('message', { op: 'subscribe', channel: 'orderbook_BUM3' });
});
conn.on('message', function (data) {
console.log('Incoming message: ' + data.private);
});
});
</script>
这将连接并通知我是否成功,但没有收到任何消息,或输出任何错误消息。我对Socket.IO还比较陌生,有什么我应该做的不同吗 问题在于SocketIO4Net正在连接和注册的默认命名空间。排序的答案是对on.connect事件处理程序进行一个小的更改: 为IEndPointClient(比如icbit)添加一个新变量,并在套接字连接上连接“icbit”命名空间 SocketIO4Net默认的“On”处理程序也将处理名称空间事件,或者您选择直接向给定端点注册它们。(即使使用附加的命名空间连接,也会建立单个连接)。您可以阅读有关SO4N的更多信息 如果查看示例html页面示例中的websocket框架跟踪,您将看到它使用的是“icbit”命名空间: 请看看这个小的解决方法是否能解决问题……我进一步研究了为什么使用querystring参数/名称空间连接,并将其发布到项目中
Jim你能分享一下socket.io服务器端的简单片段吗?我很乐意看一看(Socketio4net的作者)我没有服务器端代码-我正在尝试使用icbit.seThanks提供的API来获得帮助-我已经实现了这一点,并收到了通用套接字消息(聊天日志)由SocketMessage处理程序处理。我仍然没有看到从orderbook_BUM3订阅中收到任何消息,但是从网页运行时有很多消息。有什么特别的订阅方式吗?太棒了!我要试一试。非常感谢您的详细帮助
socket = new Client(http://api-url); // url to the nodejs / socket.io instance
socket.Opened += SocketOpened;
socket.Message += SocketMessage;
socket.SocketConnectionClosed += SocketConnectionClosed;
socket.Error += SocketError;
// register for 'connect' event with io server
socket.On("connect", (fn) =>
{
socket.Emit("message", new { op = "subscribe", channel = "orderbook_BUM3"
});
});
socket.On("message", (data) =>
{
Console.WriteLine("message received");
});
// make the socket.io connection
socket.Connect();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using SocketIOClient;
using WebSocket4Net;
namespace Console_ICBIT
{
public class SampleClient
{
private Client socket;
private IEndPointClient icbit;
private string authKey = "{your key here}";
private string userId = "{your user id here}";
public void Execute()
{
Console.WriteLine("Starting SocketIO4Net Client Events Example...");
string ioServer = string.Format("https://api.icbit.se/icbit?AuthKey={0}&UserId={1}", authKey, userId);
socket = new Client(ioServer);
socket.Opened += SocketOpened;
socket.Message += SocketMessage;
socket.SocketConnectionClosed += SocketConnectionClosed;
socket.Error += SocketError;
// register for 'connect' event with io server
socket.On("connect", (fn) =>
{ // connect to namespace
icbit = socket.Connect("/icbit");
icbit.On("connect", (cn) => icbit.Emit("message", new { op = "subscribe", channel = "orderbook_BUM3" }));
});
// make the socket.io connection
socket.Connect();
}
void SocketOpened(object sender, EventArgs e)
{
Console.WriteLine("SocketOpened\r\n");
Console.WriteLine("Connected to ICBIT API server!\r\n");
}
public void subscribe()
{
//conn.Emit("message", new { op = "subscribe", channel = "orderbook_BUH3" }); // for BTCUSD futures
//conn.Emit("message", "{op = 'subscribe', channel = 'orderbook_3' }"); // for currency exchange section BTC/USD pair
}
void SocketError(object sender, ErrorEventArgs e)
{
Console.WriteLine("socket client error:");
Console.WriteLine(e.Message);
}
void SocketConnectionClosed(object sender, EventArgs e)
{
Console.WriteLine("WebSocketConnection was terminated!");
}
void SocketMessage(object sender, MessageEventArgs e)
{
// uncomment to show any non-registered messages
if (string.IsNullOrEmpty(e.Message.Event))
Console.WriteLine("Generic SocketMessage: {0}", e.Message.MessageText);
else
Console.WriteLine("Generic SocketMessage: {0} : {1}", e.Message.Event, e.Message.Json.ToJsonString());
}
public void Close()
{
if (this.socket != null)
{
socket.Opened -= SocketOpened;
socket.Message -= SocketMessage;
socket.SocketConnectionClosed -= SocketConnectionClosed;
socket.Error -= SocketError;
this.socket.Dispose(); // close & dispose of socket client
}
}
}
}