// 发送消息
	    public void sendMessage(object msg,int messageID)
	    {
	        byte[] data;
	        using (MemoryStream ms = new MemoryStream())
	        {
	            ProtoModelSerializer serializer = new ProtoModelSerializer();
	            serializer.Serialize(ms, msg);
	            data = new byte[ms.Length];
	            ms.Position = 0;
	            ms.Read(data, 0, data.Length);
	        }

	        VitByteArray arr = new VitByteArray();
	        arr.WriteInt(data.Length +4);
	        arr.WriteInt(messageID);

	        if (data != null)
	        {
	            arr.WriteBytes(data);
	        }

	        try
	        {
	            socket.Send(arr.Buffer);
	        }
	        catch (SocketException e)
	        {
	            Debug.Log(" " + e.ErrorCode + " " + e.Message);
	        }
	    }
Exemple #2
0
	    // 处理协议
	    public int OnMessage(VitSocketModel module)
	    {
	        int protocol = module.messageID;
	        if (!ProtocolIdMap.ContainsKey(protocol))
	        {
	            Debug.Log("OnMessage instance Error [" + protocol + "]");
	            return 0;
	        }
	        if (!CallbackMap.ContainsKey(protocol))
	        {
	            Debug.Log("OnMessage callback Error [" + protocol + "]");
	            return 0;
	        }
	        
	        // 通过反射 创建probuf对象
	        Type insType = ProtocolIdMap[protocol];
	        object insObj = Activator.CreateInstance(insType);
	        // 解析协议内容
	        ProtoModelSerializer serializer = new ProtoModelSerializer();
	        System.IO.MemoryStream memStream = new System.IO.MemoryStream(module.message);
	        insObj = serializer.Deserialize(memStream, insObj, insType);

	        // 调用协议处理回调函数
	        return CallbackMap[protocol].OnMessage(insObj);
	    }
Exemple #3
0
	    // POST请求 - protobuf
	    IEnumerator POST(string url, object msg, int messageID)
	    {
			Log.DebugInfo ("发送请求................... messageID == " + messageID);
	        byte[] data;
	        using (MemoryStream ms = new MemoryStream())
	        {
	            ProtoModelSerializer serializer = new ProtoModelSerializer();
	            serializer.Serialize(ms, msg);
				Log.DebugInfo ("serializer...");
	            data = new byte[ms.Length];
	            ms.Position = 0;
	            ms.Read(data, 0, data.Length);
	        }
			System.Text.UTF8Encoding converter = new System.Text.UTF8Encoding();

//			string token = "555566";
			byte[] tokeyarray = converter.GetBytes(Utils.m_instance.m_useerTokey);

			VitByteArray arr = new VitByteArray();			
			arr.WriteInt(messageID);
			arr.WriteShort (tokeyarray.Length);//token
			arr.WriteBytes (tokeyarray);

//	        arr.WriteInt(data.Length);
	        if (data != null)
	        {
	            arr.WriteBytes(data);
	        }

//			Debug.Log ("send...." + url + "  " + arr.Length + "   " + data.Length + "   tokeyarray " + tokeyarray.Length);

	        WWW www = new WWW(url, arr.Buffer);

	        yield return www;

	        if (www.error != null)
	        {
	            //POST请求失败
				Log.DebugInfo("error is :" + www.error);
	        }
	        else
	        {
	            parseMessage(www.bytes);
	        }
	    }