private async Task <IEnumerable <StompMessage> > ReceiveMessages() { var received = await GetReceivedQueue(); if (!received.Any()) { return(Enumerable.Empty <StompMessage>()); } var result = new Queue <StompMessage>(); while (received.TryDequeue(out var item)) { var message = _ser.Deserialize(item.String); while (int.Parse(message.Headers[StompMessageHeader.ContentLength]) > item.Length) { while (received.Count == 0) { foreach (var next in await GetReceivedQueue()) { received.Enqueue(next); } } received.TryDequeue(out var added); item += added; message = _ser.Deserialize(item.String); } result.Enqueue(message); } return(result); }
void Ws_OnMessage(object sender, MessageEventArgs e) { this.BeginInvoke((Action) delegate() { StompMessage msg = serializer.Deserialize(e.Data); if (msg.Command == "MESSAGE") { AppMessage appMessage = JsonConvert.DeserializeObject <AppMessage>(msg.Body); string command = appMessage.command; switch (command) { case "GET_DEVICE_LIST": GetDeviceList(appMessage); break; case "DEVICE_ONLINE": SetDeviceOnline(appMessage); break; case "LOCATION_UPDATED": LocationUpdated(appMessage); break; case "UPDATE_INFO": UpdateInfo(appMessage); break; } } }); }
public void Send(string message) { if (_client.OnMessage != null) { _client.OnMessage(_serializer.Deserialize(message)); } }
private void WebSocketOnMessage(object sender, MessageEventArgs e) { try { var deserializedStompMessage = Serializer.Deserialize(e.Data); var stompMessageBody = deserializedStompMessage.Body; if (stompMessageBody.Contains("ping")) { Console.WriteLine($"---------------\n{DateTime.Now}: Got ping!"); UserSocketPairs.TryGetValue(_user, out var webSocket); var pingAnswer = new StompMessage("MESSAGE", "answer") { ["destination"] = "/topic/status", ["message-id"] = messageId++.ToString(), ["content-type"] = "text/plain" }; webSocket.Send(Serializer.Serialize(pingAnswer)); return; } if (stompMessageBody.Contains("answer")) { Console.WriteLine($"---------------\n{DateTime.Now}: Answer sent!"); return; } Console.WriteLine($"---------------\n{DateTime.Now}\nUser [{_user.chatId}]:\n{e.Data}"); if (!stompMessageBody.IsNullOrEmpty()) { try { var json = JsonConvert.DeserializeObject <VideoJson>(stompMessageBody); TomBot.BotClient.SendTextMessageAsync(_user.chatId, $"Vod ID: {json.vodId}\n" + $"Date: {json.date}\n" + $"UUID: {json.uuid}\n" + $"Started by: {json.startedBy}\n" + $"State: {json.state}\n" + $"twitch.tv/videos/{json.vodId}"); } catch (JsonReaderException) { } } } catch (Exception exception) { Console.WriteLine(exception); } }
/// <summary> /// 接收消息回调 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void OnMessage(object sender, MessageEventArgs e) { StompMessage msg = serializer.Deserialize(e.Data); if (msg.Command == StompFrame.CONNECTED) //连接状态 { //onMessage(e.Data); SubscribeStomp(); } else if (msg.Command == StompFrame.MESSAGE) //返回消息 { //var text = e.Data; onMessage(msg.Body);//msg.Body:消息主体 e.Data:包含请求头和消息主体 } }
void ws_OnMessage(object sender, MessageEventArgs e) { Console.WriteLine("-----------------------------"); Console.WriteLine(" ws_OnMessage says: " + e.Data); StompMessage msg = serializer.Deserialize(e.Data); if (msg.Command == StompFrame.CONNECTED) { UpdateListBox(e.Data); SubscribeStomp(); } else if (msg.Command == StompFrame.MESSAGE) { UpdateListBox(e.Data); } }
/// <summary> /// Initializes a new instance of the <see cref = "StompWebsocketClient" /> class. /// </summary> /// <param name = "socket">The socket.</param> public StompWebsocketClient(IWebSocketConnection socket) { if (socket == null) { throw new ArgumentNullException("socket"); } _socket = socket; socket.OnClose = () => { if (OnClose != null) { OnClose(); } }; socket.OnMessage = message => OnMessage(_messageSerializer.Deserialize(message)); }
/// <summary> /// Connects to the server on the specified address. /// </summary> /// <param name = "address">The address.</param> public void Connect() { _transport.OnOpen += () => _transport.Send(_serializer.Serialize(new StompMessage("CONNECT"))); _transport.OnMessage += (msg) => HandleMessage(_serializer.Deserialize(msg)); _transport.Connect(); }
//INIT - SET TEAMDATA public bool InitialzationTeam() { JObject obj = getUser("teamCode"); if (obj != null) { var client = new WebClient(); client.Headers[HttpRequestHeader.ContentType] = "application/json"; client.Encoding = UTF8Encoding.UTF8; string response = client.UploadString("http://danny-dataserver.kro.kr:8080/getUserTeam", obj.ToString()); /* * need value * * teamCode * */ JObject data = JObject.Parse(response); TeamData.id = Convert.ToInt64(data["id"].ToString()); TeamData.name = data["name"].ToString(); TeamData.code = data["code"].ToString(); TeamData.files = data["files"].ToString().Equals("null") ? null : JsonConvert.DeserializeObject <string[]>(data["files"].ToString());//.Values().Select(x => x.Value<string>()).ToArray(); string str = getNoticeMessage(); TeamData.notice = str.Equals("{\"message\":\"NO NOTICES\",\"errorCode\":\"404\"}") ? null : JsonConvert.DeserializeObject <List <Notice> >(str); string str2 = getToDoList(); TeamData.toDoList = str2.Equals("{\"message\":\"NO ToDoList\",\"errorCode\":\"404\"}") ? null : JsonConvert.DeserializeObject <List <ToDoList> >(str2); UserData.socket = new WebSocketSharp.WebSocket("ws://danny-dataserver.kro.kr:8080/my-ws/websocket"); StompMessageSerializer serializer = new StompMessageSerializer(); UserData.socket.OnOpen += (sender, e) => { var connect = new StompMessage("CONNECT"); connect["accept-version"] = "1.1"; connect["heart-beat"] = "0,1000"; UserData.socket.Send(serializer.Serialize(connect)); var sub = new StompMessage(StompFrame.SUBSCRIBE); sub["id"] = "sub-" + UserData.id; sub["destination"] = "/topics/event"; UserData.socket.Send(serializer.Serialize(sub)); var test = new StompMessage(StompFrame.SEND, "{\"name\":\"Test\"}"); test["content-type"] = "application/json"; test["destination"] = "/app/hello"; UserData.socket.Send(serializer.Serialize(test)); }; UserData.socket.OnMessage += (sender, e) => { string deserialized = serializer.Deserialize(e.Data).Body; if (deserialized.Contains("Hello")) { Console.WriteLine("Hello~"); } if (deserialized.Contains("UPDATED_FILES")) // UPDATE FILES { if (UserData.teamCode == JObject.Parse(deserialized)["name"].ToString()) { client = new WebClient(); client.Headers[HttpRequestHeader.ContentType] = "application/json"; client.Encoding = UTF8Encoding.UTF8; response = client.UploadString("http://danny-dataserver.kro.kr:8080/getUserTeam", obj.ToString()); data = JObject.Parse(response); Console.WriteLine("Received"); TeamData.files = data["files"].ToString().Equals("null") ? null : JsonConvert.DeserializeObject <string[]>(data["files"].ToString());//.Values().Select(x => x.Value<string>()).ToArray() UserData.fileUpdated = true; TeamData.Updated = true; } } if (deserialized.Contains("SetNoticeRead")) // Some player reading... { var strs = getNoticeMessage(); TeamData.notice = strs.Equals("{\"message\":\"NO NOTICES\",\"errorCode\":\"404\"}") ? null : JsonConvert.DeserializeObject <List <Notice> >(strs); } if (deserialized.Contains("AddNotices")) // Notice added { var strs = getNoticeMessage(); List <Notice> result = new List <Notice>(); TeamData.AddNotices = strs.Equals("{\"message\":\"NO NOTICES\",\"errorCode\":\"404\"}") ? null : JsonConvert.DeserializeObject <List <Notice> >(strs); if (TeamData.notice != null) { foreach (Notice n in TeamData.notice) { foreach (Notice n2 in TeamData.AddNotices) { if (n.id == n2.id) { result.Add(n2); } } } foreach (Notice n in result) { TeamData.AddNotices.Remove(n); } } UserData.noticeUpdated = true; TeamData.notice = strs.Equals("{\"message\":\"NO NOTICES\",\"errorCode\":\"404\"}") ? null : JsonConvert.DeserializeObject <List <Notice> >(strs); } if (deserialized.Contains("AddMemos")) // Memo Added { JObject objs = JObject.Parse(JObject.Parse(deserialized)["name"].ToString()); if (objs["id"].ToString().Equals(UserData.id.ToString())) // IF THIS USER { List <int> current = JsonConvert.DeserializeObject <List <int> >(objs["messages"].ToString()); UserData.memos = current.ToArray(); UserData.updateMemos = current.ToArray()[current.Count() - 1]; UserData.userMemos = GetMemoText(UserData.memos.Length - 1); UserData.memoUpdated = true; } } }; UserData.socket.OnError += (sender, e) => { }; UserData.socket.OnClose += (sender, e) => { MessageBox.Show("SERVER DISCONNECTED"); System.Diagnostics.Process.GetCurrentProcess().Kill(); }; UserData.socket.Connect(); return(true); } return(false); }