private void Read(IAsyncResult status) { if (!ConnectionSocket.Connected) return; DataFrame dr = new DataFrame(ReceivedDataBuffer); try { string messageReceived; if (!_isDataMasked) { UTF8Encoding decoder = new UTF8Encoding(); int startIndex = 0; int endIndex = 0; while (ReceivedDataBuffer[startIndex] == _firstByte[0]) startIndex++; endIndex = startIndex + 1; while (ReceivedDataBuffer[endIndex] != _lastByte[0] && endIndex != _maxBufferSize - 1) endIndex++; if (endIndex == _maxBufferSize - 1) endIndex = _maxBufferSize; messageReceived = decoder.GetString(ReceivedDataBuffer, startIndex, endIndex - startIndex); } else { messageReceived = dr.Text; } if ((messageReceived.Length == _maxBufferSize && messageReceived[0] == Convert.ToChar(65533)) || messageReceived.Length == 0) { } else { if (DataReceived != null) { DataReceived(this, messageReceived, EventArgs.Empty); } Array.Clear(ReceivedDataBuffer, 0, ReceivedDataBuffer.Length); ConnectionSocket.BeginReceive(ReceivedDataBuffer, 0, ReceivedDataBuffer.Length, 0, new AsyncCallback(Read), null); } } catch (Exception ex) { Console.WriteLine(ex); } }
/// <summary> /// 单点发送 /// </summary> /// <param name="sender"></param> /// <param name="message"></param> /// <param name="e"></param> public void DataRecivedCallBack(Object sender, string message, EventArgs e) { Logger.Info("Get Query:" + message); SocketConnection item = sender as SocketConnection; if (!item.ConnectionSocket.Connected) return; string msg = _processMessage(message); try { if (item.IsDataMasked) { DataFrame dr = new DataFrame(msg); item.ConnectionSocket.Send(dr.GetBytes()); } else { item.ConnectionSocket.Send(_firstByte); item.ConnectionSocket.Send(Encoding.UTF8.GetBytes(msg)); item.ConnectionSocket.Send(_lastByte); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void Read(IAsyncResult status) { if (!ConnectionSocket.Connected) { return; } string messageReceived = string.Empty; DataFrame dr = new DataFrame(receivedDataBuffer); try { if (!this.isDataMasked) { // Web Socket protocol: messages are sent with 0x00 and 0xFF as padding bytes System.Text.UTF8Encoding decoder = new System.Text.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 messageReceived = decoder.GetString(receivedDataBuffer, startIndex, endIndex - startIndex); } else { messageReceived = dr.Text; } if ((messageReceived.Length == MaxBufferSize && messageReceived[0] == Convert.ToChar(65533)) || messageReceived.Length == 0) { } else { if (DataReceived != null) { DataReceived(this, messageReceived, EventArgs.Empty); } Array.Clear(receivedDataBuffer, 0, receivedDataBuffer.Length); ConnectionSocket.BeginReceive(receivedDataBuffer, 0, receivedDataBuffer.Length, 0, new AsyncCallback(Read), null); } } catch (Exception ex) { Console.WriteLine(ex); } }