Пример #1
0
 private void notifyPong(WebSocketFrame frame)
 {
     try {
         listener.OnPong(frame.getPayload(), 0, frame.getPayloadLength());
     } catch (Exception) {
         // discard exceptions thrown by the listener
     }
 }
Пример #2
0
 private void notifyMessage(WebSocketFrame frame)
 {
     try {
         if (frame.getOpCode() == WebSocketFrameConstants.TEXT)
         {
             listener.OnMessage(frame.getPayloadAsString());
         }
         else
         {
             listener.OnMessage(frame.getPayload(), 0, frame.getPayloadLength());
         }
     } catch (Exception) {
         // discard exceptions thrown by the listener
     }
 }
Пример #3
0
        private void dispatch()
        {
            WebSocketFrame frame        = new WebSocketFrame();
            WebSocketFrame partialFrame = null;

            byte[] buffer = new byte[128 * 1024];

            while (Interlocked.Read(ref state) != CLOSED)
            {
                int readBytes = read(buffer);

                MemoryStream s = new MemoryStream(buffer, 0, readBytes);
                while (frame.parse(s, readBytes))
                {
                    switch (frame.getOpCode())
                    {
                    case WebSocketFrameConstants.CONTINUATION:
                        if (partialFrame == null)
                        {
                            throw new WebSocketException("unexpected continuation frame");
                        }
                        break;

                    case WebSocketFrameConstants.TEXT:
                        if (partialFrame != null)
                        {
                            throw new WebSocketException("expected continuation frame");
                        }
                        if (frame.isFin())
                        {
                            notifyMessage(frame);
                        }
                        else
                        {
                            partialFrame = frame.copy();
                        }
                        break;

                    case WebSocketFrameConstants.BINARY:
                        if (partialFrame != null)
                        {
                            throw new WebSocketException("expected continuation frame");
                        }
                        if (frame.isFin())
                        {
                            notifyMessage(frame);
                        }
                        else
                        {
                            partialFrame = frame.copy();
                        }
                        break;

                    case WebSocketFrameConstants.CLOSE:
                        notifyClose(frame.getCloseCode(), frame.getCloseReason());
                        break;

                    case WebSocketFrameConstants.PING:
                        write(WebSocketFrame.pong(frame.getPayload(), 0, frame.getPayloadLength()));
                        break;

                    case WebSocketFrameConstants.PONG:
                        notifyPong(frame);
                        break;
                    }
                }
            }
        }