예제 #1
0
 internal void RunCallback(string name, WebSocketMessageContext data)
 {
     if (this.NamedEvents.TryGetValue(name, out WebSocketCallback callback))
     {
         callback(data);
     }
 }
예제 #2
0
        internal WebSocketConnection(WebSocketModule module, HttpListenerContext ctx)
        {
            WebSocketContext wctx = ctx.AcceptWebSocketAsync(null).Result;

            using (WebSocket ws = wctx.WebSocket)
            {
                this.socket = ws;
                this.isOpen = true;
                this.module = module;
                this.Join(module.MainChannel);
                module.RunConnectionCallback(new WebSocketMessageContext(new byte[] { }, this, module));

                WebSocketReceiveBuffer buffer = new WebSocketReceiveBuffer(null);
                while (ws.State == WebSocketState.Open)
                {
                    buffer.Clear();
                    this.socketResult = ws.ReceiveAsync(buffer.Data, CancellationToken.None).Result;
                    if (this.socketResult.MessageType == WebSocketMessageType.Close)
                    {
                        this.isOpen = false;
                        Task closing = ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
                        module.RunDisconnectionCallback(new WebSocketMessageContext(new byte[] { }, this, module));
                        closing.Wait();
                        break;
                    }
                    WebSocketMessageContext wsm = new WebSocketMessageContext(buffer, this, module);
                    switch (buffer.Type)
                    {
                    case WebSocketReceiveBuffer.MessageType.Numbered:
                        module.RunCallback(buffer.ID, wsm);
                        break;

                    case WebSocketReceiveBuffer.MessageType.Named:
                        module.RunCallback(buffer.Name, wsm);
                        break;

                    default:
                        throw new WebSocketException("The WebSocket message was not compliant with Alabaster WebSocket protocol.");
                    }
                }
            }
        }
예제 #3
0
 internal void RunDisconnectionCallback(WebSocketMessageContext data) => this.disconnectEvent?.Invoke(data);
예제 #4
0
 internal void RunCallback(byte number, WebSocketMessageContext data) => this.NumberedEvents[number]?.Invoke(data);