void sendEchoMessage(TransportProtocol protocol) { FunapiSession.Transport transport = session_.GetTransport(protocol); if (transport == null) { FunDebug.LogWarning("sendEchoMessage - transport is null."); return; } ++echo_id_; if (transport.encoding == FunEncoding.kJson) { Dictionary <string, object> message = new Dictionary <string, object>(); message["message"] = string.Format("[{0}] echo message ({1})", client_id_, echo_id_); session_.SendMessage("echo", message, protocol); } else if (transport.encoding == FunEncoding.kProtobuf) { PbufEchoMessage echo = new PbufEchoMessage(); echo.msg = string.Format("[{0}] echo message ({1})", client_id_, echo_id_); FunMessage message = FunapiMessage.CreateFunMessage(echo, MessageType.pbuf_echo); session_.SendMessage("pbuf_echo", message, protocol); } }
void onReceivedMessage(string type, object message) { if (type == "echo" || type == "pbuf_echo") { --sending_count_; if (sending_count_ <= 0) { is_done_ = true; } } if (type == "echo") { Dictionary <string, object> json = message as Dictionary <string, object>; FunDebug.Log("[{0}] received: {1} (left:{2})", client_id_, json["message"] as string, sending_count_); } else if (type == "pbuf_echo") { FunMessage msg = message as FunMessage; object obj = FunapiMessage.GetMessage(msg, MessageType.pbuf_echo); if (obj == null) { return; } PbufEchoMessage echo = obj as PbufEchoMessage; FunDebug.Log("[{0}] received: {1} (left:{2})", client_id_, echo.msg, sending_count_); } }
public void sendEchoMessage(TransportProtocol protocol = TransportProtocol.kDefault) { if (!session_.Connected && !session_.ReliableSession) { FunDebug.Log("You should connect first."); return; } if (encoding_ == FunEncoding.kJson) { // In this example, we are using Dictionary<string, object>. // But you can use your preferred Json implementation (e.g., Json.net) instead of Dictionary, // by changing FunapiMessage.JsonHelper property. Dictionary <string, object> message = new Dictionary <string, object>(); message["message"] = string.Format("[{0}] hello json", protocol.ToString().Substring(1).ToLower()); session_.SendMessage("echo", message, protocol); } else if (encoding_ == FunEncoding.kProtobuf) { PbufEchoMessage echo = new PbufEchoMessage(); echo.msg = string.Format("[{0}] hello proto", protocol.ToString().Substring(1).ToLower()); FunMessage message = FunapiMessage.CreateFunMessage(echo, MessageType.pbuf_echo); session_.SendMessage(MessageType.pbuf_echo, message, protocol); } }
private void onMessageReceived(string msg_type, object message) { MessageType msgType; if (!!!Enum.TryParse(msg_type, out msgType)) { Debug.LogWarning("Unregistered msg type - " + msg_type); return; } var msg = FunapiMessage.GetMessage( message as funapi.network.fun_message.FunMessage, msgType); if (msg == null) { Debug.LogError("Cannot parse msg - " + msg + "as " + msgType); return; } #if DEBUG Debug.LogFormat("[MsgDump] <color=blue>{0}</color> : {1}", msg_type, MiniJSON.Json.Serialize(msg)); #endif base.notifyServerMessage(msgType, msg); }
void onMulticastChannelReceived(string channel_id, string sender, object body) { if (multicast_.encoding == FunEncoding.kJson) { string channel = FunapiMessage.JsonHelper.GetStringField(body, "_channel"); FunDebug.Assert(channel != null && channel == channel_id); string message = FunapiMessage.JsonHelper.GetStringField(body, "_message"); FunDebug.Log("Received a multicast message from the '{0}' channel.\nMessage: {1}", channel_id, message); } else { FunDebug.Assert(body is FunMulticastMessage); FunMulticastMessage mcast_msg = body as FunMulticastMessage; FunDebug.Assert(channel_id == mcast_msg.channel); object obj = FunapiMessage.GetMessage(mcast_msg, MulticastMessageType.pbuf_hello); PbufHelloMessage hello_msg = obj as PbufHelloMessage; if (hello_msg == null) { return; } FunDebug.Log("Received a multicast message from the '{0}' channel.\nMessage: {1}", channel_id, hello_msg.message); } }
void OnEventHandler(SNResultCode result) { FunDebug.DebugLog1("EVENT: Facebook ({0})", result); switch (result) { case SNResultCode.kLoggedIn: { var token = Facebook.Unity.AccessToken.CurrentAccessToken; if (encoding_ == FunEncoding.kJson) { Dictionary <string, object> message = new Dictionary <string, object>(); message["facebook_uid"] = token.UserId; message["facebook_access_token"] = token.TokenString; session_.SendMessage("login", message); } else if (encoding_ == FunEncoding.kProtobuf) { FacebookLoginMessage login = new FacebookLoginMessage(); login.facebook_uid = token.UserId; login.facebook_access_token = token.TokenString; FunMessage message = FunapiMessage.CreateFunMessage(login, MessageType.facebook_login); session_.SendMessage("facebook_login", message); } } break; case SNResultCode.kError: FunDebug.Assert(false); break; } }
public void OnButtonSendEcho() { if (session == null) { return; } FunapiSession.Transport transport = session.GetTransport(info.protocol); if (transport == null) { FunDebug.LogWarning("sendEchoMessage - transport is null."); return; } if (transport.encoding == FunEncoding.kJson) { Dictionary <string, object> message = new Dictionary <string, object>(); message["message"] = string.Format("[{0}] hello", transport.str_protocol); session.SendMessage("echo", message, info.protocol); } else if (transport.encoding == FunEncoding.kProtobuf) { PbufEchoMessage echo = new PbufEchoMessage(); echo.msg = string.Format("[{0}] hello", transport.str_protocol); FunMessage message = FunapiMessage.CreateFunMessage(echo, MessageType.pbuf_echo); session.SendMessage("pbuf_echo", message, info.protocol); } }
void onReceivedMessage(string type, object message) { if (type == "echo") { FunDebug.Assert(message is Dictionary <string, object>); Dictionary <string, object> json = message as Dictionary <string, object>; FunDebug.Log("Received an echo message: {0}", json["message"]); } else if (type == "pbuf_echo") { FunMessage msg = message as FunMessage; PbufEchoMessage echo = FunapiMessage.GetMessage <PbufEchoMessage>(msg, MessageType.pbuf_echo); FunDebug.Log("Received an echo message: {0}", echo.msg); } }
void onEchoWithProtobuf(object message) { FunDebug.Assert(message is FunMessage); FunMessage msg = message as FunMessage; object obj = FunapiMessage.GetMessage(msg, MessageType.pbuf_echo); if (obj == null) { return; } PbufEchoMessage echo = obj as PbufEchoMessage; FunDebug.Log("Received an echo message: {0}", echo.msg); }
public void SendMessage(TransportProtocol protocol, string message) { if (protocol == TransportProtocol.kTcp) { PbufEchoMessage echo = new PbufEchoMessage(); echo.msg = message; FunMessage fmsg = FunapiMessage.CreateFunMessage(echo, MessageType.pbuf_echo); session_.SendMessage(MessageType.pbuf_echo, fmsg, protocol); } else { Dictionary <string, object> echo = new Dictionary <string, object>(); echo["message"] = message; session_.SendMessage("echo", echo, protocol); } }
void onReceivedMessage(string type, object message) { if (type == "echo") { Dictionary <string, object> json = message as Dictionary <string, object>; FunDebug.Log("[{0}] received: {1}", client_id, json["message"] as string); } else if (type == "pbuf_echo") { FunMessage msg = message as FunMessage; object obj = FunapiMessage.GetMessage(msg, MessageType.pbuf_echo); if (obj == null) { return; } PbufEchoMessage echo = obj as PbufEchoMessage; FunDebug.Log("[{0}] received: {1}", client_id, echo.msg); } }
void onReceivedMessage(string type, object message) { if (type == "echo") { Dictionary <string, object> json = message as Dictionary <string, object>; string echo_msg = json["message"] as string; FunDebug.Log("[{0}:{2}] {1}", id_, echo_msg, ++message_number_); } else if (type == "pbuf_echo") { FunMessage msg = message as FunMessage; object obj = FunapiMessage.GetMessage(msg, MessageType.pbuf_echo); if (obj == null) { return; } PbufEchoMessage echo = obj as PbufEchoMessage; FunDebug.Log("[{0}:{2}] {1}", id_, echo.msg, ++message_number_); } }
void OnReceive(string type, object obj) { FunEncoding encoding = session_.GetEncoding(); string result = ""; if (type == "fb_authentication") { if (encoding == FunEncoding.kJson) { Dictionary <string, object> message = obj as Dictionary <string, object>; result = message["result"].ToString(); } else if (encoding == FunEncoding.kProtobuf) { FunMessage msg = obj as FunMessage; PbufAnotherMessage message = FunapiMessage.GetMessage <PbufAnotherMessage>(msg, MessageType.pbuf_another); result = message.msg; } if (result == "ok") { logged_in_ = true; if (image_ != null && facebook_.MyPicture != null) { image_.texture = facebook_.MyPicture; } setButtonState(true); } else { FunDebug.Log("facebook login authenticatiion failed."); facebook_.Logout(); } } }
void sendEcho(TransportProtocol protocol) { FunapiSession.Transport transport = session.GetTransport(protocol); if (transport == null) { return; } if (transport.encoding == FunEncoding.kJson) { Dictionary <string, object> message = new Dictionary <string, object>(); message["message"] = string.Format("[{0}] hello", transport.str_protocol); session.SendMessage("echo", message, protocol); } else if (transport.encoding == FunEncoding.kProtobuf) { PbufEchoMessage echo = new PbufEchoMessage(); echo.msg = string.Format("[{0}] hello", transport.str_protocol); FunMessage message = FunapiMessage.CreateFunMessage(echo, MessageType.pbuf_echo); session.SendMessage("pbuf_echo", message, protocol); } }
public void sendMulticastMessage() { if (multicast_.encoding == FunEncoding.kJson) { Dictionary <string, object> mcast_msg = new Dictionary <string, object>(); mcast_msg["_channel"] = kChannelName; mcast_msg["_bounce"] = true; mcast_msg["_message"] = "multicast test message"; multicast_.SendToChannel(mcast_msg); } else { PbufHelloMessage hello_msg = new PbufHelloMessage(); hello_msg.message = "multicast test message"; FunMulticastMessage mcast_msg = FunapiMessage.CreateFunMessage(hello_msg, MulticastMessageType.pbuf_hello); mcast_msg.channel = kChannelName; mcast_msg.bounce = true; multicast_.SendToChannel(mcast_msg); } }
void onMaintenanceMessage(object message) { if (encoding_ == FunEncoding.kJson) { JsonAccessor json_helper = FunapiMessage.JsonHelper; FunDebug.Log("Maintenance message\nstart: {0}\nend: {1}\nmessage: {2}", json_helper.GetStringField(message, "date_start"), json_helper.GetStringField(message, "date_end"), json_helper.GetStringField(message, "messages")); } else if (encoding_ == FunEncoding.kProtobuf) { FunMessage msg = message as FunMessage; object obj = FunapiMessage.GetMessage(msg, MessageType.pbuf_maintenance); if (obj == null) { return; } MaintenanceMessage maintenance = obj as MaintenanceMessage; FunDebug.Log("Maintenance message\nstart: {0}\nend: {1}\nmessage: {2}", maintenance.date_start, maintenance.date_end, maintenance.messages); } }
public override void SendMessage <TPacket>(MessageType packetType, TPacket packet) { _session.SendMessage(packetType, FunapiMessage.CreateFunMessage(packet, packetType)); }