Beispiel #1
0
        private void OnClientConnect(IAsyncResult asyn)
        {
            // create a new socket for the connection
            var clientSocket = ListenerSocker.EndAccept(asyn);

            // shake hands to give the new client a warm welcome
            //ShakeHands(clientSocket);

            // oh joy we have a connection - lets tell everybody about it
            LogLine(DateTime.Now + "> new connection from " + clientSocket.LocalEndPoint, ServerLogLevel.Subtle);

            // keep track of the new guy
            var clientConnection = new WebSocketConnection(clientSocket);

            Connections.Add(clientConnection);
            clientConnection.Disconnected += new WebSocketDisconnectedEventHandler(ClientDisconnected);

            // invoke the connection event
            if (ClientConnected != null)
            {
                ClientConnected(clientConnection, EventArgs.Empty);
            }

            if (LogLevel != ServerLogLevel.Nothing)
            {
                clientConnection.DataReceived += new DataReceivedEventHandler(DataReceivedFromClient);
            }

            // listen for more clients
            ListenForClients();
        }
Beispiel #2
0
        private void OnClientConnect(IAsyncResult asyn)
        {
            byte[] buffer         = new byte[1024];
            string headerResponse = "";

            // create a new socket for the connection
            var clientSocket = ListenerSocker.EndAccept(asyn);
            var i            = clientSocket.Receive(buffer);

            headerResponse = (System.Text.Encoding.UTF8.GetString(buffer)).Substring(0, i);
            //Console.WriteLine(headerResponse);


            if (clientSocket != null)
            {
                // Console.WriteLine("HEADER RESPONSE:"+headerResponse);
                var key = headerResponse.Replace("ey:", "`")
                          .Split('`')[1]                             // dGhlIHNhbXBsZSBub25jZQ== \r\n .......
                          .Replace("\r", "").Split('\n')[0]          // dGhlIHNhbXBsZSBub25jZQ==
                          .Trim();
                var test1    = AcceptKey(ref key);
                var newLine  = "\r\n";
                var name     = "Charmaine";
                var response = "HTTP/1.1 101 Switching Protocols" + newLine
                               + "Upgrade: websocket" + newLine
                               + "Connection: Upgrade" + newLine
                               + "Sec-WebSocket-Accept: " + test1 + newLine + newLine
                               + "Testing lang naman po:" + name
                ;

                // which one should I use? none of them fires the onopen method
                clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(response));
            }



            // keep track of the new guy
            var clientConnection = new WebSocketConnection(clientSocket);

            Connections.Add(clientConnection);
            // clientConnection.Disconnected += new WebSocketDisconnectedEventHandler(ClientDisconnected);
            Console.WriteLine("New user: " + ipLocal);
            // invoke the connection event
            if (ClientConnected != null)
            {
                ClientConnected(clientConnection, EventArgs.Empty);
            }

            if (LogLevel != ServerLogLevel.Nothing)
            {
                clientConnection.DataReceived += new DataReceivedEventHandler(DataReceivedFromClient);
            }



            // listen for more clients
            ListenForClients();
        }