コード例 #1
0
        private void messageCallback(IAsyncResult ar)
        {
            var sw = ar.AsyncState as SocketWrapper;

            try
            {
                sw.workSocket.EndReceive(ar);

                int bytesReceived = sw.workSocket.Receive(sw.buffer);
                if (bytesReceived < sw.buffer.Length)
                {
                    Array.Resize <byte>(ref sw.buffer, bytesReceived);
                }

                if (bytesReceived == 0)
                {
                    AddMessage(String.Format("{0} 客户端退出", sw.workSocket.RemoteEndPoint.ToString()));
                    proxSockets.Remove(sw);
                }
                else
                {
                    EOpcodeType opcode = WebSocketHelper.GetFrameOpcode(sw.buffer);

                    // If the connection was closed
                    if (opcode == EOpcodeType.ClosedConnection)
                    {
                        sw.workSocket.Close();
                        return;
                    }

                    AddMessage(string.Format("接收到客户端:{0} 的消息是:{1}", sw.workSocket.RemoteEndPoint.ToString(), WebSocketHelper.GetDataFromFrame(sw.buffer)));
                    sw.buffer = new Byte[SocketWrapper.BUFFER_SIZE];
                    sw.workSocket.BeginReceive(new byte[0], 0, 0, SocketFlags.None, messageCallback, sw);
                }
            }
            catch (Exception ex)
            {
                sw.workSocket.Close();
                sw.workSocket.Dispose();
                Console.WriteLine(ex.Message);
            }
        }
コード例 #2
0
        private void UpgradeCallback(IAsyncResult ar)
        {
            var sw = ar.AsyncState as SocketWrapper;

            try
            {
                sw.workSocket.EndReceive(ar);
                Int32 bytesReceived = sw.workSocket.Receive(sw.buffer);
                if (bytesReceived == 0)
                {
                    AddMessage(String.Format("{0}客户端退出", sw.workSocket.RemoteEndPoint.ToString()));
                    proxSockets.Remove(sw);
                }
                else
                {
                    if (!sw.Upgraded)
                    {
                        string requestKey       = WebSocketHelper.GetHandshakeRequestKey(Encoding.Default.GetString(sw.buffer));
                        string hanshakeResponse = WebSocketHelper.GetHandshakeResponse(WebSocketHelper.HashKey(requestKey));
                        sw.workSocket.Send(Encoding.Default.GetBytes(hanshakeResponse));
                        sw.Upgraded = true;

                        AddMessage(string.Format("客户端:{0} 升级为 WebSocket 成功", sw.workSocket.RemoteEndPoint.ToString(), Encoding.Default.GetString(sw.buffer, 0, bytesReceived)));
                    }
                    sw.workSocket.BeginReceive(new byte[0], 0, 0, SocketFlags.None, messageCallback, sw);
                }
            }
            catch (Exception ex)
            {
                sw.workSocket.Close();
                sw.workSocket.Dispose();
                Console.WriteLine(ex.Message);
            }
        }