/// <summary> /// Event fired when a data is received from the Alchemy Websockets server instance. /// Parses data as JSON and calls the appropriate message or sends an error message. /// </summary> /// <param name="context">The user's connection context</param> public void OnReceive(UserContext context) { try { var json = context.DataFrame.ToString(); RequestToTossServer obj = JsonConvert.DeserializeObject <RequestToTossServer>(json); ResponseFromTossServer r; switch (obj.Type) { case CommandType.RegisterReq: Register(obj.userIdentity, context); break; case CommandType.HeartBeatReq: var user = OnlineUsers.Keys.Where(o => o.Context.ClientAddress == context.ClientAddress).Single(); r = new ResponseFromTossServer() { Type = CommandType.HeartBeatRes }; user.Context.Send(JsonConvert.SerializeObject(r)); break; default: break; } } catch (Exception e) // Bad JSON! For shame. { var r = new ResponseFromTossServer { Type = CommandType.Error, Data = new { e.Message } }; context.Send(JsonConvert.SerializeObject(r)); } }
/// <summary> /// Register a user's context for the first time with a username, and add it to the list of online users /// </summary> /// <param name="name">The name to register the user under</param> /// <param name="context">The user's connection context</param> private void Register(string identify, UserContext context) { var json = context.DataFrame.ToString(); RequestToTossServer obj = JsonConvert.DeserializeObject <RequestToTossServer>(json); var u = OnlineUsers.Keys.Where(o => o.Context.ClientAddress == context.ClientAddress).Single(); u.Identity = obj.userIdentity; //OnlineUsers[u] = obj.userIdentity; ResponseFromTossServer r = new ResponseFromTossServer() { Type = CommandType.RegisterRes }; var sjson = JsonConvert.SerializeObject(r); context.Send(sjson); }
void wsClient_OnHeartBeatTickEvent(Object sender) { RequestToTossServer data = new RequestToTossServer() { Type = Com.Huen.Sockets.CommandType.HeartBeatReq, userIdentity = util.loginuserinfo.IDENTITY }; wsClient.Send(JsonConvert.SerializeObject(data)); }