Пример #1
0
	//適当なCloseフレームを送信し、切断する。
	public void Disconnect()
	{

		try
		{
			//切断応答
			var close_fragment_header = new WebSocketFragmentInfo();
			close_fragment_header.FIN = true;
			close_fragment_header.Opcode = WebSocketFragmentOpcode.ConnectionClose;
			close_fragment_header.MASK = false;
			close_fragment_header.PayloadLengthBits = PayloadLengthBits.Bits_7;

			byte[] binary = close_fragment_header.ToBinary();
			lock(send_lock_object)
			{
				stream.Write(binary,0,binary.Length);
			}

			Socket.Shutdown(SocketShutdown.Both);
		}finally{
			stream.Close();
			Socket.Close();
		}
	}
Пример #2
0
	public void SendMessage(WebSocketMessage msg)
	{
		//フラグメントヘッダを生成する
		var wsfi = new WebSocketFragmentInfo();
		wsfi.FIN = true;
		wsfi.Opcode = msg.IsTextData ?
		WebSocketFragmentOpcode.Text : 
		WebSocketFragmentOpcode.Binary;
		
		wsfi.MASK = false;
		
		byte[] data = msg.GetData();

		if(data.Length <= 125)
		{
			wsfi.PayloadLengthBits = PayloadLengthBits.Bits_7;
		}else if(data.Length <= 65535){
			wsfi.PayloadLengthBits = PayloadLengthBits.Bits_23;
		}else{
			wsfi.PayloadLengthBits = PayloadLengthBits.Bits_71;
		}

		wsfi.PayloadLength = (ulong)data.LongLength;
		byte[] fragment_header_data = wsfi.ToBinary();

		//データを送信。
		lock(send_lock_object)
		{
			stream.Write(fragment_header_data,0,fragment_header_data.Length);
			stream.Write(data,0,data.Length);
		}
	}