Exemplo n.º 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();
        }
Exemplo n.º 2
0
 public void Connect()
 {
     isDispose = false;
     try
     {
         if (ListenerSocker == null)
         {
             ListenerSocker = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         }
         ListenerSocker.SendTimeout    = 2000;
         ListenerSocker.ReceiveTimeout = 2000;
         ListenerSocker.BeginConnect(uri.Host, uri.Port, new AsyncCallback(ConnectCallback), this);
     }
     catch
     {
         Thread.Sleep(2000);
         if (countReintentosSetUpServer < 10)
         {
             Connect();
             countReintentosSetUpServer++;
         }
         else
         {
             OnClientDisconnected(this, new EventArgs());
         }
     }
 }
Exemplo n.º 3
0
        public void Dispose()
        {
            try
            {
                if (isDispose)
                {
                    return;
                }
                if (ListenerSocker != null)
                {
                    try
                    {
                        if (ListenerSocker.Connected)
                        {
                            ListenerSocker.Disconnect(false);
                        }
                    }
                    catch { }

                    ListenerSocker.Close();
                    ListenerSocker.Dispose();
                }
            }
            catch { }

            ListenerSocker = null;

            ClientConnected = null;
            DataReceived    = null;
            DataSent        = null;
            isDispose       = true;
        }
Exemplo n.º 4
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();
        }
Exemplo n.º 5
0
 public void Send(byte[] _buffer)
 {
     if (isDispose)
     {
         return;
     }
     try
     {
         if (ListenerSocker != null)
         {
             ListenerSocker.BeginSend(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(SendCallBack), this);
         }
     }
     catch
     {
         OnClientDisconnected(this, new EventArgs());
     }
 }
Exemplo n.º 6
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            if (isDispose)
            {
                return;
            }
            int sizeOfReceivedData = -1;

            try
            {
                sizeOfReceivedData = ListenerSocker.EndReceive(ar);
            }
            catch { }

            if (sizeOfReceivedData > 0)
            {
                OnDataReceived(this, new DataReceivedEventArgs(sizeOfReceivedData, _buffer));
                Listen();
            }
            else // the socket is closed
            {
            }
        }
Exemplo n.º 7
0
 private void Listen()
 {
     try
     {
         if (ListenerSocker != null)
         {
             ListenerSocker.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceiveCallback, this);
         }
     }
     catch
     {
         Thread.Sleep(2000);
         if (countReintentosListen < 10)
         {
             Listen();
             countReintentosListen++;
         }
         else
         {
             OnClientDisconnected(this, new EventArgs());
         }
     }
 }
Exemplo n.º 8
0
 // look for connecting clients
 private void ListenForClients()
 {
     ListenerSocker.BeginAccept(new AsyncCallback(OnClientConnect), null);
 }