c#
  • socket.io
  • socketio4net
  • 2013-04-13 1 views 0 likes 
    0

    SocketIO4net을 사용하여 socket.io API를 사용하려고하지만 메시지를 수신하지 못하는 것 같습니다. 이 잘 작동하고 API에 연결하고 메시지를받을 수 있습니다SocketIO4net에서받은 메시지가 없습니다.

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

    : 같은 VS2010 솔루션에서 나는 다음과 같은 코드를 가진 웹 사이트가있다. 이 연결하고 그러나 어떤 메시지가 수신되고 있지 성공의 저를 알리는 또는 오류 메시지

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

    출력 : 나는 그때에 다음 코드를 삽입 한 SocketIO4Net 라이브러리 및 테스트 프로젝트가 있습니다. 나는 소켓에 처음 온 사람이야. 다르게해야 할 일이 있을까?

    +0

    당신은 socket.io 서버 측의 베어 뼈 조각을 공유 할 수, 그리고 내가 서버 측 코드가없는 모양 (Socketio4net의 저자) –

    +0

    을 드리겠습니다 - – Macros

    답변

    2

    문제는 기본 네임 스페이스 SocketIO4Net에 &에 등록하는 것입니다. 정렬 대답은 on.connect 이벤트 처리기에서 약간의 변경을하는 것입니다. IEndPointClient (icbit)에 대한 새 변수를 추가하고 소켓 연결에서 "icbit"네임 스페이스로 연결합니다.

    SocketIO4Net 기본 "On"처리기는 네임 스페이스 이벤트도 처리하거나 지정된 끝점에 직접 등록하도록 선택합니다. (추가 네임 스페이스 연결을 사용하더라도 단일 연결이 이루어집니다.) 당신은 샘플 HTML 페이지 샘플에서 웹 소켓 프레임 추적 보면 당신은 SO4N namespace

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

    에 대한 자세한 내용을보실 수 있습니다 - 당신은이 "icbit"네임 스페이스를 사용하고 볼 수 있습니다 : orderbook_BUH3

    참조하십시오 이 작은 해결 방법으로 문제가 해결되지 않으면 ... querystring 매개 변수/네임 스페이스 연결이 프로젝트 site에 게시 된 이유를 자세히 살펴 봅니다.

    +0

    도움을 주셔서 감사합니다 -이 구현 한 및 SocketMessage 처리기에서 처리하는 일반 소켓 메시지 (채팅 로그) 나타납니다. orderbook_BUM3 구독에서받은 메시지가 아직 표시되지 않지만 웹 페이지에서 실행 중일 때는 많은 메시지가 표시됩니다. 구독해야하는 특정 방법이 있습니까? – Macros

    +0

    여기에서 일하는 전체 예제를 보여주기 위해 위의 샘플을 업데이트했습니다 ... –

    +0

    뛰어난! 나는 이걸 줄거야. 자세한 도움말을 가져 주셔서 감사합니다. – Macros

    관련 문제