簡體   English   中英

SocketIO4net中未收到任何消息

[英]No messages received in 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'></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很新,有什么我應該做的不同嗎?

問題是SocketIO4Net正在連接和注冊的默認命名空間。 排序答案是在on.connect事件處理程序中進行一些小改動:為IEndPointClient(比如icbit)添加一個新變量,在套接字連接上,連接“icbit”命名空間。

SocketIO4Net默認的“On”處理程序也會處理命名空間事件,或者您選擇直接在給定端點上注冊它們。 (即使使用其他命名空間連接,也會建立單個連接)。 您可以閱讀有關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”命名空間: orderbook_BUH3

請查看這個小的解決方法是否無法解決您的問題...我會進一步了解為什么查詢字符串params / namespace連接並在項目站點上發布

吉姆

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM