public async Task Subscribe(JToken data, IWebsocketCallback webcallback) { userId = ObjectId.Parse(data["id"].ToString()); ClientCallback callback = new ClientCallback(webcallback); subscribers.TryAdd(userId, callback); await ChangeStatus(Status.Web); ICommunicationObject obj = (ICommunicationObject)webcallback; obj.Closed += async(s, e) => { await Unsubscribe(); }; }
public void UnSubscribe(string Id) { IWebsocketCallback callback = null; try { if (!oSubscription.ContainsKey(Id)) { if (!oSubscription.TryRemove(Id, out callback)) { } } } catch (Exception ex) { throw new Exception(ex); } }
public async Task SendMessageToServer(System.ServiceModel.Channels.Message msg) { if (msg.IsEmpty) { Console.WriteLine("received empty message"); return; } byte[] body = msg.GetBody <byte[]>(); string clientMessage = Encoding.UTF8.GetString(body); JObject jsonMessage = JObject.Parse(clientMessage); string type = jsonMessage["type"].ToString(); JToken data = jsonMessage["data"]; switch (type) { case Message: await chatService.SendMessage(data); break; case FriendshipRequest: await friendService.AddFriend(data); break; case FriendshipAccept: await friendService.ReplyToFriendRequest(data); break; case Login: IWebsocketCallback webCallback = OperationContext.Current.GetCallbackChannel <IWebsocketCallback>(); await loginService.Subscribe(data, webCallback); break; default: Console.WriteLine(String.Format("{0} type is not supported", type)); break; } }
public async void Response(string Id) { IWebsocketCallback callback = null; List <string> oKeys; try { oKeys = oSubscription.Keys.ToList <string>(); foreach (string key in oKeys) { if (!oSubscription.TryGetValue(key, out callback)) { throw new Exception(""); } if (((IChannel)callback).State != CommunicationState.Opened) { throw new Exception(""); } string msgTextToClient = string.Format("RFQRESPONSE|{0}|{1}", callback, callback); await SendMessageToServer(CreateMessage(msgTextToClient)); } } catch (Exception ex) { throw new Exception(ex); } }
public ClientCallback(IWebsocketCallback websocketCallback) { this.desktopCallback = null; this.websocketCallback = websocketCallback; }
public ClientCallback(IServerCallback serverCallback) { this.desktopCallback = serverCallback; this.websocketCallback = null; }