Ejemplo n.º 1
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.");
                    }
                }
            }
        }
Ejemplo n.º 2
0
 internal WebSocketChannel(WebSocketModule module, long id)
 {
     this.Module = module;
     this.id     = id;
     this.Name   = "Channel " + id;
 }
Ejemplo n.º 3
0
 internal WebSocketMessageContext(byte[] data, WebSocketConnection connection, WebSocketModule module)
 {
     this.Data       = data;
     this.Connection = connection;
     this.Module     = module;
 }
Ejemplo n.º 4
0
 public static void AttachWebSocketModule(string route, WebSocketModule module)
 {
     Get(route, (Request req) => (req.IsWebSocketRequest) ? (Response)(new WebSocketHandshake(module, req.cw.Context)) : PassThrough.Skip);
     attachedWSModules.Add(module);
 }
Ejemplo n.º 5
0
 public WebSocketHandshake(WebSocketModule module, HttpListenerContext ctx)
 {
     WebSocketConnection connection = new WebSocketConnection(module, ctx);
 }
Ejemplo n.º 6
0
 internal WebSocketMessageContext(WebSocketReceiveBuffer dataBuffer, WebSocketConnection connection, WebSocketModule module)
 {
     this.Data       = dataBuffer.Data.ToArray();
     this.Connection = connection;
     this.Module     = module;
 }