public void Send(string data) { if (Socket.Connected) { Socket.AsyncSend(DataFrame.Wrap(data), (byteCount) => { Log.Debug(byteCount + " bytes send to " + Socket.RemoteEndPoint); }); } else { WebSocket.Disconnected(); Socket.Close(); } }
/// <summary> /// Asynchronously send data to the client /// </summary> /// <param name="data">the data to send</param> public void Send(string data) { if (Socket.Connected) { var bytes = Encoding.UTF8.GetBytes(data); var fragment = DataFragment.Create(bytes, DataFragment.FragmentHead.Fin, DataFragment.FragmentOpcode.Text, false); Socket.AsyncSend(fragment.GetBytes(), (byteCount) => { Log.Debug(byteCount + " bytes send to " + Socket.RemoteEndPoint); }); } else { OnDisconnect(this, null); Socket.Close(); } }
private void BeginSendServerHandshake(ServerHandshake handshake, Socket socket) { var stringShake = "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: " + handshake.Accept + "\r\n"; if (!string.IsNullOrEmpty(handshake.SubProtocol)) { stringShake += "Sec-WebSocket-Protocol: " + handshake.SubProtocol + "\r\n"; } stringShake += "\r\n"; // generate a byte array representation of the handshake byte[] byteResponse = Encoding.ASCII.GetBytes(stringShake); socket.AsyncSend(byteResponse, (size) => { if (_onSuccess != null) { _onSuccess(ClientHandshake); } }); }
public void SendAsync(string data) { Socket.AsyncSend(Encoding.UTF8.GetBytes(data)); }