Ejemplo n.º 1
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);
		}
	}