private void InteractiveClient_HelloMethodHandler(object sender, MethodPacket e) { if (e.method.Equals("hello")) { this.Connected = true; } }
/// <summary> /// Authenticates the currently logged in user with the channel's chat. /// </summary> /// <returns>Whether the operation succeeded</returns> public async Task <bool> Authenticate() { try { MethodPacket packet = new MethodPacket() { method = "auth", arguments = new JArray() { this.Channel.id.ToString(), this.User.id.ToString(), this.channelChat.authkey }, }; this.Authenticated = false; ReplyPacket reply = await this.SendAndListen(packet, checkIfAuthenticated : false); if (reply != null && reply.dataObject.TryGetValue("authenticated", out JToken value)) { this.Authenticated = (bool)value; } return(this.Authenticated); } catch { return(false); } }
// private void ProcessStack () { // IMessagePacket imp = mPacketStack.Dequeue (); // Debug.Log ("Processing stack :: " + imp.msgType); // switch ((MessageType) imp.msgType) { // case MessageType.START_GAME: // m_ChatBox.OnSendBtnClick (mMainStream); // StartChat (); // break; // case MessageType.SIMPLE_MSG: // SimpleMessagePacket sm = (SimpleMessagePacket) imp; // m_ChatBox.AddToBox (sm.fromIP + " said : " + sm.msg); // break; // default: // break; // } // } private void ProcessMethod() { MethodPacket crntPack = mMethodStack.Dequeue(); Debug.Log("method parameters:: " + crntPack.methodName + ", " + ", TOTAL PARAMS: " + crntPack.allParams.Length); Type t = this.GetType(); MethodInfo theMethodToCall = t.GetMethod(crntPack.methodName); if (theMethodToCall == null) { Debug.LogError("No Such Method exists:: " + crntPack.methodName); } else { Debug.Log("total number of args :: " + theMethodToCall.GetParameters().Count()); Debug.Log("incoming args count :: " + crntPack.allParams.Length); if (theMethodToCall.GetParameters().Count() != crntPack.allParams.Length) { Debug.LogError("ARGUMENT COUNT MISMATCH ::"); } else { if (crntPack.allParams.Length > 0) { theMethodToCall.Invoke(this, crntPack.allParams); } else { theMethodToCall.Invoke(this, null); } } } }
private void InteractiveClient_ReadyMethodHandler(object sender, MethodPacket e) { JToken value; if (e.method.Equals("onReady") && e.parameters.TryGetValue("isReady", out value) && (bool)value) { this.Authenticated = true; } }
private void AssignLatestSequence(WebSocketPacket packet) { if (packet is MethodPacket) { MethodPacket mPacket = (MethodPacket)packet; mPacket.seq = this.lastSequenceNumber; } else if (packet is ReplyPacket) { ReplyPacket rPacket = (ReplyPacket)packet; rPacket.seq = this.lastSequenceNumber; } }
/// <summary> /// Processes a JSON packet received from the server. /// </summary> /// <param name="packet">The packet JSON</param> /// <returns>An awaitable Task</returns> protected override Task ProcessReceivedPacket(string packet) { List <JToken> packetJTokens = new List <JToken>(); JToken packetJToken = JToken.Parse(packet); if (packetJToken is JArray) { foreach (JToken t in (JArray)packetJToken) { packetJTokens.Add(t); } } else { packetJTokens.Add(packetJToken); } foreach (JToken token in packetJTokens) { WebSocketPacket webSocketPacket = token.ToObject <WebSocketPacket>(); string data = JSONSerializerHelper.SerializeToString(token); this.OnPacketReceivedOccurred?.Invoke(this, webSocketPacket); if (webSocketPacket.type.Equals("method")) { MethodPacket methodPacket = JsonConvert.DeserializeObject <MethodPacket>(data); this.SendSpecificPacket(methodPacket, this.OnMethodOccurred); } else if (webSocketPacket.type.Equals("reply")) { ReplyPacket replyPacket = JsonConvert.DeserializeObject <ReplyPacket>(data); if (this.replyIDListeners.ContainsKey(replyPacket.id)) { this.replyIDListeners[replyPacket.id] = replyPacket; } this.SendSpecificPacket(replyPacket, this.OnReplyOccurred); } else if (webSocketPacket.type.Equals("event")) { EventPacket eventPacket = JsonConvert.DeserializeObject <EventPacket>(data); this.SendSpecificPacket(eventPacket, this.OnEventOccurred); } } return(Task.FromResult(0)); }
protected override Task ProcessReceivedPacket(string packetJSON) { dynamic jsonObject = JsonConvert.DeserializeObject(packetJSON); List <WebSocketPacket> packets = new List <WebSocketPacket>(); if (jsonObject.Type == JTokenType.Array) { JArray array = JArray.Parse(packetJSON); foreach (JToken token in array.Children()) { packets.Add(token.ToObject <WebSocketPacket>()); } } else { packets.Add(JsonConvert.DeserializeObject <WebSocketPacket>(packetJSON)); } foreach (WebSocketPacket packet in packets) { if (this.OnPacketReceivedOccurred != null) { this.OnPacketReceivedOccurred(this, packet); } if (packet.type.Equals("method")) { MethodPacket methodPacket = JsonConvert.DeserializeObject <MethodPacket>(packetJSON); this.SendSpecificPacket(methodPacket, this.OnMethodOccurred); } else if (packet.type.Equals("reply")) { ReplyPacket replyPacket = JsonConvert.DeserializeObject <ReplyPacket>(packetJSON); this.AddReplyPacketForListeners(replyPacket); this.SendSpecificPacket(replyPacket, this.OnReplyOccurred); } else if (packet.type.Equals("event")) { EventPacket eventPacket = JsonConvert.DeserializeObject <EventPacket>(packetJSON); this.SendSpecificPacket(eventPacket, this.OnEventOccurred); } } return(Task.FromResult(0)); }
protected void WebSocketClient_OnPacketSentOccurred(object sender, WebSocketPacket e) { if (e is MethodPacket) { MethodPacket mPacket = (MethodPacket)e; Logger.Log(string.Format(Environment.NewLine + "WebSocket Method Sent: {0} - {1} - {2} - {3} - {4}" + Environment.NewLine, e.id, e.type, mPacket.method, mPacket.arguments, mPacket.parameters)); } else if (e is ReplyPacket) { ReplyPacket rPacket = (ReplyPacket)e; Logger.Log(string.Format(Environment.NewLine + "WebSocket Reply Sent: {0} - {1} - {2} - {3} - {4}" + Environment.NewLine, e.id, e.type, rPacket.result, rPacket.error, rPacket.data)); } else { Logger.Log(string.Format(Environment.NewLine + "WebSocket Packet Sent: {0} - {1}" + Environment.NewLine, e.id, e.type)); } }
/// <summary> /// These functions are running on a different thread, not in the main thread, /// so if any error happens, i wont get anything on console unless used with try/catch Debug.Log function /// </summary> private void OnMsgReceivedByClient(string _msg) { mIsGameStarted = true; byte[] receivedBytes = NetworkUtil.GetBytes(_msg); Debug.Log(_msg); Debug.Log("received by mono " + Encoding.ASCII.GetString(receivedBytes)); try { MemoryStream memStream = new MemoryStream(); BinaryFormatter binForm = new BinaryFormatter(); memStream.Write(receivedBytes, 0, receivedBytes.Length); memStream.Seek(0, SeekOrigin.Begin); MethodPacket obj = (MethodPacket)binForm.Deserialize(memStream); mMethodStack.Enqueue(obj); Debug.Log("Method successfully enqued..."); } catch (Exception _exc) { Debug.LogError("ERROR in processing method packet: " + _exc.Message); } //====Right below, it was the initial prototype without using C# reflection, it was becoming difficult to maintain with packets, //====so genuinly felt the need to use a more robust way to implement rpc calls // IMessagePacket mp = JsonUtility.FromJson<IMessagePacket>(_msg); // Debug.Log("Client received :: " + mp); // switch ((MessageType)mp.msgType) // { // case MessageType.START_GAME: // isGameStarted = true; // packetStack.Enqueue(mp); // break; // case MessageType.SIMPLE_MSG: // SimpleMessagePacket smp = JsonUtility.FromJson<SimpleMessagePacket>(_msg); // packetStack.Enqueue(smp); // break; // default: // break; // } }
/// <summary> /// Prepares the client to receive interactive events. /// </summary> /// <returns>Whether the operation succeeded</returns> public async Task <bool> Ready() { this.Authenticated = false; this.OnMethodOccurred += InteractiveClient_ReadyMethodHandler; JObject parameters = new JObject(); parameters.Add("isReady", true); MethodPacket packet = new MethodPacket() { method = "ready", parameters = parameters, discard = true }; await this.Send(packet, checkIfAuthenticated : false); await this.WaitForResponse(() => { return(this.Authenticated); }); this.OnMethodOccurred -= InteractiveClient_ReadyMethodHandler; return(this.Authenticated); }
private void InteractiveClient_OnMethodOccurred(object sender, MethodPacket methodPacket) { this.lastSequenceNumber = Math.Max(methodPacket.seq, this.lastSequenceNumber); switch (methodPacket.method) { case "issueMemoryWarning": this.SendSpecificMethod(methodPacket, this.OnIssueMemoryWarning); break; case "onParticipantLeave": this.SendSpecificMethod(methodPacket, this.OnParticipantLeave); break; case "onParticipantJoin": this.SendSpecificMethod(methodPacket, this.OnParticipantJoin); break; case "onParticipantUpdate": this.SendSpecificMethod(methodPacket, this.OnParticipantUpdate); break; case "onGroupCreate": this.SendSpecificMethod(methodPacket, this.OnGroupCreate); break; case "onGroupDelete": if (this.OnGroupDelete != null) { Tuple <InteractiveGroupModel, InteractiveGroupModel> groupDeleted = new Tuple <InteractiveGroupModel, InteractiveGroupModel>( new InteractiveGroupModel() { groupID = methodPacket.parameters["groupID"].ToString() }, new InteractiveGroupModel() { groupID = methodPacket.parameters["reassignGroupID"].ToString() }); this.OnGroupDelete(this, groupDeleted); } break; case "onGroupUpdate": this.SendSpecificMethod(methodPacket, this.OnGroupUpdate); break; case "onSceneCreate": this.SendSpecificMethod(methodPacket, this.OnSceneCreate); break; case "onSceneDelete": if (this.OnSceneDelete != null) { Tuple <InteractiveConnectedSceneModel, InteractiveConnectedSceneModel> sceneDeleted = new Tuple <InteractiveConnectedSceneModel, InteractiveConnectedSceneModel>( new InteractiveConnectedSceneModel() { sceneID = methodPacket.parameters["sceneID"].ToString() }, new InteractiveConnectedSceneModel() { sceneID = methodPacket.parameters["reassignSceneID"].ToString() }); this.OnSceneDelete(this, sceneDeleted); } break; case "onSceneUpdate": this.SendSpecificMethod(methodPacket, this.OnSceneUpdate); break; case "onControlCreate": this.SendSpecificMethod(methodPacket, this.OnControlCreate); break; case "onControlDelete": this.SendSpecificMethod(methodPacket, this.OnControlDelete); break; case "onControlUpdate": this.SendSpecificMethod(methodPacket, this.OnControlUpdate); break; case "giveInput": this.SendSpecificMethod(methodPacket, this.OnGiveInput); break; } }
protected void WebSocketClient_OnMethodOccurred(object sender, MethodPacket e) { Logger.Log(string.Format(Environment.NewLine + "WebSocket Method: {0} - {1} - {2} - {3} - {4}" + Environment.NewLine, e.id, e.type, e.method, e.arguments, e.parameters)); }
protected void SendSpecificMethod <T>(MethodPacket methodPacket, EventHandler <T> eventHandler) { this.SendSpecificPacket(JsonConvert.DeserializeObject <T>(methodPacket.parameters.ToString()), eventHandler); }
private void InteractiveClient_OnMethodOccurred(object sender, MethodPacket e) { this.methodPackets.Add(e); }
private void MixPlayClient_OnMethodOccurred(object sender, MethodPacket e) { this.methodPackets.Add(e); }