2017-03-14 2 views
0

複数のwebsocketサーバーから受信したメッセージをリッスンし、最終的にフロントエンドのない既存のアプリケーション内で他のビジネスロジックを開始するサービスを設定します。サービスとしてのWebSocket C#リスナー

私はウェブソケットを初めて使う人として、これを実装する最良の方法として未定です。

誰もが同様の設定を行っている経験がありますか?

答えて

1

良いチュートリアルが必要です。

を確認してください。this c#でwebsocketを使用するSignalRを使用しているものを確認してください。

+0

ありがとうございます。 – rfashoni

1

あなたは、あなたがIHttpHandler、サンプルを通じてWebSocket接続を処理するためにSystem.Web.WebSockets.AspNetWebSocketContextを使用することができますIISを使用している場合:あなたは、スタンドアロンのサーバーを使用している場合

/// <summary> 
/// Called when a new web socket connection has been established. 
/// </summary> 
/// <param name="webSocketContext">The web socket context.</param> 
public abstract void WebSocketContext(System.Web.WebSockets.AspNetWebSocketContext webSocketContext); 

/// <summary> 
/// Process request method. 
/// </summary> 
/// <param name="context">The current http context.</param> 
public void ProcessRequest(System.Web.HttpContext context) 
{ 
    HttpResponse response = null; 

    // Get the request and response context. 
    response = context.Response; 

    // If the request is a web socket protocol 
    if (context.IsWebSocketRequest) 
    { 
     // Process the request. 
     ProcessWebSocketRequest(context); 
    } 
} 

/// <summary> 
/// Process the request. 
/// </summary> 
/// <param name="httpContext">The http context.</param> 
private void ProcessWebSocketRequest(System.Web.HttpContext httpContext) 
{ 
    HttpResponse response = null; 

    // Get the request and response context. 
    response = httpContext.Response; 

    // Process the request asynchronously. 
    httpContext.AcceptWebSocketRequest(ProcessWebSocketRequestAsync); 
} 

/// <summary> 
/// Process the request asynchronously. 
/// </summary> 
/// <param name="webSocketContext">The web socket context.</param> 
/// <returns>The task to execute.</returns> 
private async Task ProcessWebSocketRequestAsync(System.Web.WebSockets.AspNetWebSocketContext webSocketContext) 
{ 
    await Nequeo.Threading.AsyncOperationResult<bool>. 
     RunTask(() => 
     { 
      // Process the request. 
      WebSocketContext(webSocketContext); 
     }); 
} 

(これはWindowsのサービスとして実装することができます)要求を受信した

/// <summary> 
/// The on web socket context event handler, triggered when a new connection is established or data is present. Should be used when implementing a new connection. 
/// </summary> 
public event Nequeo.Threading.EventHandler<System.Net.WebSockets.HttpListenerWebSocketContext> OnWebSocketContext; 

/// <summary> 
/// On http context action handler. 
/// </summary> 
/// <param name="context">The current http context.</param> 
private void OnHttpContextHandler(HttpListenerContext context) 
{ 
    HttpListenerRequest request = null; 
    HttpListenerResponse response = null; 

    // Get the request and response context. 
    request = context.Request; 
    response = context.Response; 

    // If the request is a web socket protocol 
    if (request.IsWebSocketRequest) 
    { 
      // Process the web socket request. 
      ProcessWebSocketRequest(context); 
    } 
} 

/// <summary> 
/// Process the web socket protocol request. 
/// </summary> 
/// <param name="context">The current http context.</param> 
private async void ProcessWebSocketRequest(HttpListenerContext context) 
{ 
    HttpListenerResponse response = null; 

    // Get the request and response context. 
    response = context.Response; 

    // When calling `AcceptWebSocketAsync` the negotiated subprotocol 
    // must be specified. This sample assumes that no subprotocol was requested. 
    HttpListenerWebSocketContext webSocketContext = await context.AcceptWebSocketAsync(subProtocol: null); 

    // Send the web socket to the handler. 
    if (OnWebSocketContext != null) 
      OnWebSocketContext(this, webSocketContext); 
} 

たらあなたは、サンプルの会話を開始します:あなたは、サンプル、HttpListenerContext「を通してのWebSocket」接続を処理するためにSystem.Net.WebSockets.HttpListenerWebSocketContextを使用することができます。

/// <summary> 
/// On WebSocket context. 
/// </summary> 
/// <param name="context"></param> 
private async void OnWebSocketContext(System.Net.WebSockets.HttpListenerWebSocketContext context) 
{  
    WebSocket webSocket = null; 

    try 
    { 
     // Get the current web socket. 
     webSocket = context.WebSocket; 

     CancellationTokenSource receiveCancelToken = new CancellationTokenSource(); 
     byte[] receiveBuffer = new byte[READ_BUFFER_SIZE]; 

     // While the WebSocket connection remains open run a 
     // simple loop that receives data and sends it back. 
     while (webSocket.State == WebSocketState.Open) 
     { 
      // Receive the next set of data. 
      ArraySegment<byte> arrayBuffer = new ArraySegment<byte>(receiveBuffer); 
      WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(arrayBuffer, receiveCancelToken.Token); 

      // If the connection has been closed. 
      if (receiveResult.MessageType == WebSocketMessageType.Close) 
      { 
        break; 
      } 
      else 
      { 
        // Start conversation. 
      } 
     } 

     // Cancel the receive request. 
     if (webSocket.State != WebSocketState.Open) 
      receiveCancelToken.Cancel(); 
    } 
    catch { } 
    finally 
    { 
     // Clean up by disposing the WebSocket. 
     if (webSocket != null) 
      webSocket.Dispose(); 
    } 
} 
関連する問題