Beispiel #1
0
        public void ShouldReturnNullWhenServerProtocolsEmpty()
        {
            var server = new string[0];
            var client = new string[] { "x", "y" };

            Assert.Null(SubProtocolNegotiator.Negotiate(server, client));
        }
Beispiel #2
0
        public void ShouldReturnFirstClientMatch()
        {
            var server = new string[] { "a", "b", "c" };
            var client = new string[] { "b", "a" };

            Assert.AreEqual("b", SubProtocolNegotiator.Negotiate(server, client));
        }
Beispiel #3
0
        public void ShouldThrowOnNoMatchesOfServer()
        {
            var server = new string[] { "a", "b", "c" };
            var client = new string[] { "z" };

            Assert.Throws <SubProtocolNegotiationFailureException>(() => SubProtocolNegotiator.Negotiate(server, client));
        }
Beispiel #4
0
        private void OnClientConnect(ISocket clientSocket)
        {
            if (clientSocket == null)
            {
                return; // socket closed
            }
            FleckLog.Debug(string.Format("Client connected from {0}:{1}", clientSocket.RemoteIpAddress, clientSocket.RemotePort.ToString()));
            ListenForClients();

            WebSocketConnection connection = null;

            connection = new WebSocketConnection(
                clientSocket,
                _config,
                bytes => RequestParser.Parse(bytes, _scheme),
                r => {
                try {
                    return(HandlerFactory.BuildHandler(
                               r, s => connection.OnMessage(s), connection.Close, b => connection.OnBinary(b),
                               b => connection.OnPing(b), b => connection.OnPong(b)
                               ));
                } catch (WebSocketException) {
                    const string responseMsg = "HTTP/1.1 200 OK\r\n"
                                               + "Date: {0}\r\n"
                                               + "Server: SharpChat\r\n"
                                               + "Content-Length: {1}\r\n"
                                               + "Content-Type: text/html; charset=utf-8\r\n"
                                               + "Connection: close\r\n"
                                               + "\r\n"
                                               + "{2}";
                    string responseBody = File.Exists(@"http-motd.txt") ? File.ReadAllText(@"http-motd.txt") : @"SharpChat";

                    clientSocket.Stream.Write(Encoding.UTF8.GetBytes(string.Format(
                                                                         responseMsg, DateTimeOffset.Now.ToString(@"r"), Encoding.UTF8.GetByteCount(responseBody), responseBody
                                                                         )));
                    clientSocket.Close();
                    return(null);
                }
            },
                s => SubProtocolNegotiator.Negotiate(SupportedSubProtocols, s));

            if (IsSecure)
            {
                FleckLog.Debug("Authenticating Secure Connection");
                clientSocket
                .Authenticate(Certificate,
                              EnabledSslProtocols,
                              connection.StartReceiving,
                              e => FleckLog.Warn("Failed to Authenticate", e));
            }
            else
            {
                connection.StartReceiving();
            }
        }