Ejemplo n.º 1
0
        public void NewConnection(IClientSender clientSender, IClientEvents clientEvents,ConnectionInfo connectionInfo)
        {
            Accept = connectionInfo.Accept;
            Path = connectionInfo.Path;
            Version = connectionInfo.Version;
            ClientSender = clientSender;
            clientEvents.OnMessage = (message) =>
                                                 {
                                                     HasNewMessage = true;
                                                     Message = Encoding.UTF8.GetString(message);
                                                 };

            clientEvents.OnClose = (reason) =>
                                                      {
                                                          HasClosedconnection = true;
                                                          CloseReasons.Add(reason);
                                                      };
            clientEvents.OnPing = (ping) =>
                                           {
                                               HasPing = true;
                                               PingMessage = ping;
                                           };
        }
Ejemplo n.º 2
0
        private void OnNewHandshakeRequest(IAsyncResult ar)
        {
            var parameters = (HandshakeParameter)ar.AsyncState;
            var requestMessageTrimmed = Encoding.UTF8.GetString(parameters.HandshakeBuffer).TrimEnd("\0".ToCharArray());
            var handshakeRequest = new HandshakeRequest(requestMessageTrimmed);
            var handshakeResponse = _handshakeResponseFactory.GetResponse(handshakeRequest);

            if (NewConnection == null)
            {
                parameters.ClientSocket.Close();
                parameters.ClientSocket.Dispose();
                return;
            }

            var messageDecoder = _messageDecoderFactory.GetDecoder(handshakeRequest.Version);
            var clientSocket = new ClientSocket(parameters.ClientSocket,new byte[parameters.MessageSize],_maskingService);
            var messageHandler = new ClientMessageHandler(clientSocket, messageDecoder,new ThreadSafeBuffer());
            var clientEvents = new ClientEvents(messageHandler);
            var clientSender = new ClientSender(messageHandler);
            var connectionInfo = new ConnectionInfo
            {
                Accept = handshakeResponse.AcceptToken,
                Path = handshakeRequest.Path,
                Version = handshakeRequest.Version
            };

            NewConnection(clientSender, clientEvents, connectionInfo);
            parameters.ClientSocket.Send(Encoding.UTF8.GetBytes(handshakeResponse.ToString()));
        }