public async Task Close(WebsocketCloseCode c = WebsocketCloseCode.Normal, string msg = "") { //Console.WriteLine(string.Format("Closing socket {0} {1}", c.ToString(), msg)); var wsf = new WebSocketFrame(); wsf.Flags = WebSocketFlags.FinalFragment; wsf.OpCode = WebSocketOpCode.ConnectionClose; var dt2 = Encoding.UTF8.GetBytes(msg ?? ""); wsf.Payload = new byte[2 + dt2.Length]; wsf.PayloadLength = (ulong)wsf.Payload.LongLength; var dt = BitConverter.GetBytes((UInt16)(int)c); wsf.Payload[0] = dt[1]; wsf.Payload[1] = dt[0]; if (dt2.Length > 0) { Buffer.BlockCopy(dt2, 0, wsf.Payload, 2, dt2.Length); } await SendFrame(wsf); _sentClose = true; }
private void End(WebsocketCloseCode c = WebsocketCloseCode.ProtocolError, string msg = null) { _ct.Cancel(); _ct.Dispose(); _ct = null; _s.Close(); _s.Dispose(); _s = null; OnDisconnect(c, msg); }