示例#1
0
        private void Client_Closing(ITcpConnection client)
        {
            client.Closing -= Client_Closing;

            _connections.TryRemove(client.RemoteEndPoint, out _);
            ConnectionClosing?.Invoke(client.RemoteEndPoint);
        }
示例#2
0
        private void ReadDataContent(IAsyncResult asyncResult)
        {
            if (!bindSocket.Connected)
            {
                return;
            }
            string    message   = string.Empty;
            DataFrame dataFrame = new DataFrame(receivedDataBuffer);

            try
            {
                if (!IsDataMasked)
                {
                    //WebSocket协议:消息以0x00和0xFF作为填充字节发送
                    UTF8Encoding encoding   = new UTF8Encoding();
                    int          startIndex = 0;
                    int          endIndex   = 0;

                    // Search for the start byte
                    while (receivedDataBuffer[startIndex] == firstByte[0])
                    {
                        startIndex++;
                    }
                    // Search for the end byte
                    endIndex = startIndex + 1;
                    while (receivedDataBuffer[endIndex] != lastByte[0] && endIndex != maxBufferSize - 1)
                    {
                        endIndex++;
                    }
                    if (endIndex == maxBufferSize - 1)
                    {
                        endIndex = maxBufferSize;
                    }
                    // Get the message
                    message = encoding.GetString(receivedDataBuffer, startIndex, endIndex - startIndex);
                }//if
                else
                {
                    message = dataFrame.Text;
                }

                if ((message.Length == maxBufferSize && message[0] == Convert.ToChar(65533)) || message.Length == 0)
                {
                    //断开连接
                    logger?.Info("message");
                    if (string.IsNullOrEmpty(message))
                    {
                        MessageInfo messageInfo = new MessageInfo()
                        {
                            MsgType = MessageType.None,
                            Message = ""
                        };
                        message = JsonConvert.SerializeObject(messageInfo);
                    }
                    ConnectionClosing?.Invoke(this, message);
                }
                else
                {
                    if (DataReceived != null)
                    {
                        logger?.Info("接收到的信息 [\"" + message + "\"]");
                        //消息发送
                        DataReceived(this, message);
                    }
                    Array.Clear(receivedDataBuffer, 0, receivedDataBuffer.Length);
                    bindSocket.BeginReceive(receivedDataBuffer, 0, receivedDataBuffer.Length, 0, ReadDataContent, null);
                }
            }
            catch (Exception ex)
            {
                logger?.Error("Socket连接将被终止", ex);
                MessageInfo messageInfo = new MessageInfo()
                {
                    MsgType = MessageType.Error,
                    Message = ex.Message + Environment.NewLine + "Socket连接将被终止"
                };
                ConnectionClosing?.Invoke(this, JsonConvert.SerializeObject(messageInfo));
            }
        }
示例#3
0
文件: CoreAPI.cs 项目: bugRanger/Chat
 private void OnConnectionClosing(IPEndPoint remote)
 {
     ConnectionClosing?.Invoke(remote);
 }
示例#4
0
        private void ConnectionClosed()
        {
            var args = new ConnectionClosingEventArgs(this);

            ConnectionClosing?.Invoke(this, args);
        }