没有在SocketIO4net收到的邮件(No messages received in Socke

2019-10-18 00:45发布

我试图用消耗然而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'></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>

这工作得很好,并可以连接到API和接收消息。 然后我有我已经把下面的代码放到SocketIO4Net库和测试项目:

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

这种连接,并通知我成功但没有接收到消息,或任何错误消息的输出。 我是相当新的Socket.IO,有什么我应该做不同?

Answer 1:

问题是默认的命名空间SocketIO4Net的连接和与注册。 排序答案尽在on.connect事件处理一个小的变化:添加一个新的变量为IEndPointClient(说icbit),和插座连接,连接的“icbit”命名空间。

该SocketIO4Net默认的“开”的处理程序将处理命名空间的事件为好,或者你选择直接与给定端点注册。 (A单一连接与所述附加的命名空间连接还送)。 你可以阅读更多关于SO4N 命名空间

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
            }
        }
    }
}

如果你从一个样本HTML页面样本的WebSocket框架跟踪 - 你会看到它使用的是“icbit”命名空间:

请看看这个小解决方法不能解决您......我深入了解一下,为什么查询字符串PARAMS /命名空间连接,并张贴在项目现场 。

吉姆



文章来源: No messages received in SocketIO4net