private void ListenToRequests() { var buffer = new byte[1]; var stream = new List <byte>(); while (_client.Connected) { var count = _client.Client.Receive(buffer); if (count <= 0) { continue; } if (buffer[0] == 0) { var total = stream.ToArray(); var messageString = Encoding.UTF8.GetString(total); var msg = StompMessage.FromString(messageString); stream.Clear(); if (OnReceived != null) { OnReceived(this, msg); } } else { stream.Add(buffer[0]); } } }
private void HandleNotification(object sender, StompMessage msg) { try { if (!msg.Type.Equals(ServerActions.Notification)) { return; } // TODO: handle notification here var type = msg.Headers["type"]; var subforum = msg.Headers["subforum"]; DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IEnumerable <Message>)); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(msg.Content))) { IEnumerable <Message> messages = (IEnumerable <Message>)serializer.ReadObject(stream); Notification notification = new Notification { SubForum = subforum, Type = NotifictionType.RefreshAll, Messages = messages }; if (OnNotification != null) { OnNotification(this, notification); } } } catch (Exception ex) { } }
protected void FillError(StompMessage msg) { if (msg.Headers.ContainsKey("error")) { Error = msg.Headers["error"]; } }
public void Send(StompMessage message) { var buffer = Encoding.UTF8.GetBytes(message.ToString()); _client.Client.Send(buffer); var zero = new byte[] { 0 }; _client.Client.Send(zero); }
private void HandleLoginResponse(object server, StompMessage msg) { if (msg.Type != ServerActions.LoginMember) { return; } _client.OnReceived -= HandleLoginResponse; _waitEvent.Set(); }
private void HandleRemoveSubForumResponse(object sender, StompMessage e) { if (e.Type != ServerActions.RemoveSubForum) { return; } FillError(e); _client.OnReceived -= HandleRemoveSubForumResponse; _waitEvent.Set(); }
private void HandleEditMessageResponse(object sender, StompMessage msg) { if (msg.Type != ServerActions.EditMessage) { return; } FillError(msg); _client.OnReceived -= HandleEditMessageResponse; _waitEvent.Set(); }
private void HandleRemoveFriendRequest(object sender, StompMessage msg) { if (msg.Type != ServerActions.RemoveFriend) { return; } FillError(msg); _client.OnReceived -= HandleRemoveFriendRequest; _waitEvent.Set(); }
private void HandleIsSystemInitializedResponse(object server, StompMessage msg) { if (msg.Type != ServerActions.IsSystemInitialized) { return; } FillError(msg); _client.OnReceived -= HandleIsSystemInitializedResponse; _initialized = bool.Parse(msg.Content); _waitEvent.Set(); }
private void HandleLoginGuestRequest(object sender, StompMessage e) { if (e.Type != ServerActions.LoginGuest) { return; } FillError(e); _client.OnReceived -= HandleLoginGuestRequest; _sid = e.Content; _waitEvent.Set(); }
private void HandleGetSubForumsResponse(object sender, StompMessage msg) { if (msg.Type != ServerActions.GetSubForums) { return; } FillError(msg); _subforums = msg.Content.Trim().Split('\n'); _client.OnReceived -= HandleGetSubForumsResponse; _waitEvent.Set(); }
private void HandleLoginGuestResponse(object sender, StompMessage msg) { if (msg.Type != ServerActions.LoginGuest) { return; } FillError(msg); _sessionId = msg.Headers["sid"]; _client.OnReceived -= HandleLoginGuestResponse; _waitEvent.Set(); }
public void LogIn(string username, string password) { var msg = new StompMessage { Type = ServerActions.LoginMember }; msg.Headers.Add("username", username); msg.Headers.Add("password", password); _client.OnReceived += HandleLoginResponse; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); }
public string[] GetForums() { var msg = new StompMessage { Type = ServerActions.GetForums }; _client.OnReceived += HandleGetForumsResponse; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); ValidateError(); return(_forums); }
public void LogoutMember(string sid) { var msg = new StompMessage { Type = ServerActions.LogoutMember }; msg.Headers.Add("sid", sid); _client.OnReceived += HandleLogoutMemberRequest; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); ValidateError(); }
public void InitSystem(string admin, string adminpass) { var msg = new StompMessage { Type = ServerActions.InitSystem }; msg.Headers.Add("username", admin); msg.Headers.Add("password", adminpass); _client.OnReceived += HandleInitSystemResponse; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); ValidateError(); }
public void RemoveFriend(string sid, string friend) { var msg = new StompMessage { Type = ServerActions.RemoveFriend }; msg.Headers.Add("sid", sid); msg.Headers.Add("friend", friend); _client.OnReceived += HandleRemoveFriendRequest; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); ValidateError(); }
public void RemoveForum(string adminSession, string forumName) { var msg = new StompMessage { Type = ServerActions.RemoveForum }; msg.Headers.Add("forum", forumName); msg.Headers.Add("adminSessionId", adminSession); _client.OnReceived += HandleRemoveForumResponse; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); ValidateError(); }
public string LoginGuest(string forumName) { var msg = new StompMessage { Type = ServerActions.LoginGuest }; msg.Headers.Add("forum", forumName); _sid = string.Empty; _client.OnReceived += HandleLoginGuestRequest; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); ValidateError(); return(_sid); }
public IEnumerable <Message> GetThreads(string sid, string subforum) { var msg = new StompMessage { Type = ServerActions.GetThreads }; msg.Headers.Add("sid", sid); msg.Headers.Add("subforum", subforum); _client.OnReceived += HandleGetThreadsResponse; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); ValidateError(); return(_messages); }
public string[] GetSubForums(string sid) { var msg = new StompMessage { Type = ServerActions.GetSubForums }; msg.Headers.Add("sid", sid); _subforums = null; _client.OnReceived += HandleGetSubForumsResponse; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); ValidateError(); return(_subforums); }
public void RemoveMessage(string sid, string subforum, string msgid) { var msg = new StompMessage { Type = ServerActions.RemoveMessage }; msg.Headers.Add("sid", sid); msg.Headers.Add("subforum", subforum); msg.Headers.Add("msgid", msgid); _client.OnReceived += HandleRemoveMessageResponse; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); ValidateError(); }
public void Register(string sid, string username, string password) { var msg = new StompMessage { Type = ServerActions.Register }; msg.Headers.Add("sid", sid); msg.Headers.Add("username", username); msg.Headers.Add("password", password); _client.OnReceived += HandleRegisterRequest; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); ValidateError(); }
public string LogInAdmin(string username, string password) { var msg = new StompMessage { Type = ServerActions.LoginAdmin }; msg.Headers.Add("username", username); msg.Headers.Add("password", password); _client.OnReceived += HandleLoginAdminResponse; _sid = string.Empty; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); ValidateError(); return(_sid); }
public void AddModerator(string sid, string subforum, string moderator) { var msg = new StompMessage() { Type = ServerActions.AddModerator }; msg.Headers.Add("sid", sid); msg.Headers.Add("subforum", subforum); msg.Headers.Add("moderator", moderator); _client.OnReceived += HandleAddModeratorResponse; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); ValidateError(); }
public void AddNewThread(string sid, string subforum, string title, string body) { var msg = new StompMessage { Type = ServerActions.AddNewThread }; msg.Headers.Add("sid", sid); msg.Headers.Add("subforum", subforum); msg.Headers.Add("title", title); msg.Content = body; _client.OnReceived += HandleAddNewThreadResponse; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); ValidateError(); }
public void CreateForum(string adminSession, string forumName, string manager, string password) { var msg = new StompMessage { Type = ServerActions.CreateForum }; msg.Headers.Add("forum", forumName); msg.Headers.Add("adminSessionId", adminSession); msg.Headers.Add("manager", manager); msg.Headers.Add("password", password); _client.OnReceived += HandleCreateForumResponse; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); ValidateError(); }
public bool IsSystemInitialized() { var msg = new StompMessage { Type = ServerActions.IsSystemInitialized }; _client.OnReceived += HandleIsSystemInitializedResponse; _initialized = false; _client.Send(msg); _waitEvent.WaitOne(TIMEOUT); try { ValidateError(); return(_initialized); } catch (Exception ex) { return(false); } }
private void HandleGetThreadsResponse(object sender, StompMessage msg) { if (msg.Type != ServerActions.GetThreads) { return; } FillError(msg); _client.OnReceived -= HandleGetThreadsResponse; DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IEnumerable <Message>)); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(msg.Content))) { IEnumerable <Message> messages = (IEnumerable <Message>)serializer.ReadObject(stream); //JsonValue value = JsonObject.Parse(msg.Content); _messages = messages; _waitEvent.Set(); } }
private void HandleLoginAdminResponse(object sender, StompMessage msg) { if (msg.Type != ServerActions.LoginAdmin) { return; } ValidateError(); try { _sid = msg.Headers["sid"]; } catch (Exception ex) { } finally { _client.OnReceived -= HandleLoginAdminResponse; _waitEvent.Set(); } }