public void OnMouseDown() { if (desc != null && (!joined)) { api.Play(desc, (g) => { joined = true; game = g; balance.OnBalance(); }, (m) => { string lb = m.data.GetField("label").str; if (lb.Equals("timer")) { OnTimer(m.data); } else if (lb.Equals("update")) { Debug.Log(m.data.ToString()); } else { OnNotice(m.data); } }); } else if (desc != null && joined) { JSONObject jn = new JSONObject(JSONObject.Type.OBJECT); jn.AddField("command", "onLeave"); Instance ins = new Instance(desc.ApplicationId(), game.GetField("instanceId").str, "onLeave", jn); api.Request(ins, (m) => { balance.OnBalance(); tb.text = desc.Name(); joined = false; api.StopUdp(); }); } }
public void Play(Descriptor descriptor, Action <JSONObject> callback, Action <Message> onstream) { JSONObject jn = new JSONObject(JSONObject.Type.OBJECT); jn.AddField("command", "onPlay"); jn.AddField("accessMode", 2); jn.AddField("applicationId", descriptor.ApplicationId()); Application app = new Application("presence/lobby", "onPlay", jn); Request(app, (game) => { game.AddField("applicationId", descriptor.ApplicationId()); callback(game); JSONObject conn; AddMessageListener(game.GetField("label").str, onstream);//web socket listener if ((conn = game.GetField("gameObject").GetField("connection")) != null) { conn.AddField("ticket", game.GetField("gameObject").GetField("ticket").str); conn.AddField("instanceId", game.GetField("instanceId").str); AddMessageListener(game.GetField("label").str + "#" + game.GetField("instanceId").str, onstream); initUdp(conn); } else //stream on websocket if udp not available { JSONObject ms = new JSONObject(JSONObject.Type.OBJECT); ms.AddField("action", "onStream"); ms.AddField("streaming", true); ms.AddField("path", "/application/instance"); ms.AddField("instanceId", game.GetField("instanceId").str); ms.AddField("applicationId", game.GetField("applicationId").str); JSONObject dt = new JSONObject(JSONObject.Type.OBJECT); dt.AddField("command", "onStream"); ms.AddField("data", dt); _Send(ms); } }); }