Пример #1
0
 void OnClientMessage(WebSocketConnection sender, DataReceivedEventArgs e)
 {
     User user = Users.Single(a => a.Connection == sender);
     if (e.Data.Contains("/nick"))
     {
         string[] tmpArray = e.Data.Split(new char[] { ' ' });
         if (tmpArray.Length > 1)
         {
             string myNewName = tmpArray[1];
             while (Users.Where(a => a.Name == myNewName).Count() != 0)
             {
                 myNewName += "_";
             }
             if (user.Name != null)
                 wss.SendToAll("server: '" + user.Name + "' changed name to '" + myNewName + "'");
             else
                 sender.Send("you are now know as '" + myNewName + "'");
             user.Name = myNewName;
         }
     }
     else
     {
         string name = (user.Name == null) ? unknownName : user.Name;
         wss.SendToAllExceptOne(name + ": " + e.Data, sender);
         sender.Send("me: " + e.Data);
     }
 }
Пример #2
0
 void OnClientDisconnected(WebSocketConnection sender, EventArgs e)
 {
     try
     {
         User user = Users.Single(a => a.Connection == sender);
         string name = (user.Name == null) ? unknownName : user.Name;
         wss.SendToAll("server: "+name + " disconnected");
         Users.Remove(user);
     }
     catch (Exception exc)
     {
         Console.WriteLine("ehm...");
     }
 }
Пример #3
0
        private void OnClientConnect(IAsyncResult asyn)
        {
            // create a new socket for the connection
            var clientSocket = ListenerSocker.EndAccept(asyn);


            // oh joy we have a connection - lets tell everybody about it
            LogLine(DateTime.Now + "> new connection from " + clientSocket.LocalEndPoint, ServerLogLevel.Subtle);



            // keep track of the new guy
            var clientConnection = new WebSocketConnection(clientSocket);

            // shake hands to give the new client a warm welcome
            ShakeHands(clientSocket, clientConnection);

            clientConnection.ListenIncomingData();


            Connections.Add(clientConnection);
            clientConnection.Disconnected += new WebSocketDisconnectedEventHandler(ClientDisconnected);

            // invoke the connection event
            if (ClientConnected != null)
            {
                ClientConnected(clientConnection, EventArgs.Empty);
            }

            if (LogLevel != ServerLogLevel.Nothing)
            {
                clientConnection.DataReceived += new DataReceivedEventHandler(DataReceivedFromClient);
            }



            // listen for more clients
            ListenForClients();
        }
Пример #4
0
 void OnClientConnected(WebSocketConnection sender, EventArgs e)
 {
     //.Add(new User() { Connection = sender });
     //sender.Disconnected += new WebSocketDisconnectedEventHandler(OnClientDisconnected);
     //sender.DataReceived += new DataReceivedEventHandler(OnClientMessage);
 }
Пример #5
0
 void DataReceivedFromClient(WebSocketConnection sender, DataReceivedEventArgs e)
 {
     Log(DateTime.Now + "> data from " + sender.ConnectionSocket.LocalEndPoint, ServerLogLevel.Subtle);
     Log(": " + e.Data + "\n" + e.Size + " bytes", ServerLogLevel.Verbose);
     LogLine("", ServerLogLevel.Subtle);
 }
Пример #6
0
 void ClientDisconnected(WebSocketConnection sender, EventArgs e)
 {
     Connections.Remove(sender);
     LogLine(DateTime.Now + "> " + sender.ConnectionSocket.LocalEndPoint + " disconnected", ServerLogLevel.Subtle);
 }
Пример #7
0
        private void OnClientConnect(IAsyncResult asyn)
        {
            // create a new socket for the connection
            var clientSocket = ListenerSocker.EndAccept(asyn);

            // shake hands to give the new client a warm welcome
            ShakeHands(clientSocket);

            // oh joy we have a connection - lets tell everybody about it
            LogLine(DateTime.Now + "> new connection from " + clientSocket.LocalEndPoint, ServerLogLevel.Subtle);

            // keep track of the new guy
            var clientConnection = new WebSocketConnection(clientSocket);
            Connections.Add(clientConnection);
            clientConnection.Disconnected += new WebSocketDisconnectedEventHandler(ClientDisconnected);

            // invoke the connection event
            if (ClientConnected != null)
                ClientConnected(clientConnection, EventArgs.Empty);

            if (LogLevel != ServerLogLevel.Nothing)
                clientConnection.DataReceived += new DataReceivedEventHandler(DataReceivedFromClient);

            // listen for more clients
            ListenForClients();
        }
Пример #8
0
 void DataReceivedFromClient(WebSocketConnection sender, DataReceivedEventArgs e)
 {
     Log(DateTime.Now + "> data from " + sender.ConnectionSocket.LocalEndPoint, ServerLogLevel.Subtle);
     Log(": " + e.Data + "\n" + e.Size + " bytes", ServerLogLevel.Verbose);
     LogLine("", ServerLogLevel.Subtle);
 }
Пример #9
0
 void ClientDisconnected(WebSocketConnection sender, EventArgs e)
 {
     Connections.Remove(sender);
     LogLine(DateTime.Now + "> " + sender.ConnectionSocket.LocalEndPoint + " disconnected", ServerLogLevel.Subtle);
 }
Пример #10
0
 /// <summary>
 /// send a string to all the clients except one
 /// </summary>
 /// <param name="data">the string to send</param>
 /// <param name="indifferent">the client that doesn't care</param>
 public void SendToAllExceptOne(string data, WebSocketConnection indifferent)
 {
     foreach (var client in Connections)
     {
         if (client != indifferent)
             client.Send(data);
     }
 }
Пример #11
0
 /// <summary>
 /// Accepts a new WebSocketConnection object and creates a new game <see cref="Client" /> based on it.
 /// </summary>
 /// <param name="conn">The connection to use for the new client.</param>
 public void NewClient(WebSocketConnection conn)
 {
     Clients.Add(new Client(this, conn));
 }
Пример #12
0
 void OnClientDisconnected(WebSocketConnection sender, EventArgs e)
 {
     try
     {
         OnlineUser user = Users.Single(a => a.Connection == sender);
         user.Contacts.ForEach(cid => SendToContact(cid, String.Format("OFFLINE:{0}>>", user.UserID)));
         Users.Remove(user);
     }
     catch (Exception exc)
     {
         CommunicationSystem.Logger.SetError(exc);
     }
 }
Пример #13
0
        void OnClientMessage(WebSocketConnection sender, DataReceivedEventArgs e)
        {
            OnlineUser user = Users.Single(a => a.Connection == sender);
            int index = e.Data.IndexOf(">>");
            string[] action = e.Data.Substring(0, index).Split(':');
            string[] args = action[1].Split(',');
            string data = e.Data.Remove(0, index + 2);
            Dictionary<string, string[]> actions = GetAllArgs(args);
            Func<string> combine = delegate()
            {
                List<string> newargs = new List<string>();
                foreach(KeyValuePair<string, string[]> a in actions)
                    newargs.Add(string.Format("{0}({1})", a.Key, String.Join("|", a.Value)));
                return string.Format("{0}:{1}>>{2}", action[0], string.Join(",", newargs.ToArray()), data);
            };
            switch(action[0])
            {
                case "REGISTER":
                    //auth here
                    int newuserid = int.Parse(args[0]);
                    if (Users.Exists(c => c.UserID == newuserid))
                        Users.Single(c => c.UserID == newuserid).Connection.Dispose();

                    user.UserID = newuserid;
                    user.Contacts = new List<int>();
                    ContactInfo.GetContacts(user.UserID).ForEach(delegate(ContactInfo i) { user.Contacts.Add(i.ContactID); });
                    user.Contacts.ForEach(delegate(int cid){
                        if (Users.Exists(c => c.UserID == cid))
                        {
                            SendToContact(cid, String.Format("ONLINE:{0}>>", user.UserID));
                            SendToContact(user.UserID, String.Format("ONLINE:{0}>>", cid));
                        }
                    });
                    foreach (MessageInfo undmessage in MessageInfo.GetAllUndelivered(user.UserID))
                    {
                        SendToContact(user.UserID, undmessage.MessageText);
                        UndeliveredMessageInfo.Create(undmessage.MessageID, user.UserID).Remove();
                    }
                    break;
                case "MESSAGE":
                    MessageInfo message = new MessageInfo();
                    message.UserID = user.UserID;
                    message.MessageText = e.Data;
                    message.CreatedDate = DateTime.Now;
                    if (actions["TYPE"][0] == "chat") message.MessageType = MessageTypes.Chat;
                    else if (actions["TYPE"][0] == "topic") message.MessageType = MessageTypes.Comment;
                    message.Update();
                    if (message.MessageID > 0 && actions["TYPE"][0] == "topic")
                    {
                        int tid = int.Parse(actions["ID"][0]);
                        MessageInTopicInfo.Create(message.MessageID, tid).Add();
                    }

                    int iid;
                    foreach (string id in actions["TO"])
                    {
                        if (int.TryParse(id, out iid) && user.Contacts.Contains(iid))
                        {
                            bool isOnline = Users.Exists(c => c.UserID == iid);
                            //save as undelivered
                            if (message.MessageID > 0 && !isOnline)
                                UndeliveredMessageInfo.Create(message.MessageID, iid).Add();
                            //dispatch message
                            if (isOnline)
                            {
                                OnlineUser contact = Users.Single(c => c.UserID == iid);
                                contact.Connection.Send(e.Data);
                            }
                        }
                    }
                    break;
                case "TOPICGET":
                    TopicInfo topicinfo = TopicInfo.Get(int.Parse(actions["ID"][0]));
                    List<UserInTopicInfo> usersintopic = UserInTopicInfo.GetByTopic(topicinfo.TopicID);
                    List<string> ids = new List<string>(),
                        mentors = new List<string>();
                    usersintopic.ForEach(delegate(UserInTopicInfo uit) { ids.Add(uit.UserID.ToString()); if (uit.UserTopicRole == UserTopicRoles.Mentor) mentors.Add(uit.UserID.ToString()); });
                    user.Connection.Send(string.Format("TOPIC:TO({0}),MENTORS({1}),TOPICID({2})>>{3}", string.Join("|", ids.ToArray()), string.Join("|", mentors.ToArray()), topicinfo.TopicID, topicinfo.TopicText));
                    foreach(MessageInfo topicmessageinfo in MessageInfo.GetByTopic(topicinfo.TopicID))
                    {
                        user.Connection.Send(topicmessageinfo.MessageText);
                    }
                    break;
                case "TOPIC":
                    TopicInfo info = new TopicInfo();
                    info.TopicText = data;
                    info.Update();
                    for (int i = 0; i < actions["TO"].Length; i++)
                    {
                        UserInTopicInfo usertopic = new UserInTopicInfo();
                        usertopic.UserID = int.Parse(actions["TO"][i]);
                        usertopic.TopicID = info.TopicID;
                        usertopic.UserTopicRole = (actions["MENTORS"].Contains(actions["TO"][i]) ? UserTopicRoles.Mentor : UserTopicRoles.User);
                        usertopic.Add();
                    }
                    actions.Add("TOPICID", new string[] { info.TopicID.ToString() });
                    string newdata = combine();
                    foreach (string id in actions["TO"])
                    {
                        if (int.TryParse(id, out iid) && iid != user.UserID && Users.Exists(c => c.UserID == iid))
                        {
                            OnlineUser contact = Users.Single(c => c.UserID == iid);
                            contact.Connection.Send(newdata);
                        }
                    }
                    user.Connection.Send(string.Format("TOPICREF:ID({0}),REF({1})>>{2}", info.TopicID, actions["REF"][0], info.GetTitle()));
                    break;
                case "TOPICEDIT":
                    topicinfo = TopicInfo.Get(int.Parse(actions["ID"][0]));
                    topicinfo.TopicText = data;
                    topicinfo.Update();
                    UserInfo.GetByTopic(topicinfo.TopicID).ForEach(delegate(UserInfo u) { SendToContact(u.UserID, e.Data); });
                    break;
                case "SETUSERS":
                    int setusersid = int.Parse(actions["ID"][0]);
                    string setusersdata = "";
                    if (actions["TYPE"][0] == "topic")
                    {
                        setusersdata = TopicInfo.Get(setusersid).GetTitle();
                    }
                    foreach (string setusersrej in actions["OLDUSERS"])
                    {
                        if (string.IsNullOrEmpty(setusersrej)) continue;
                        if (actions["TYPE"][0] == "topic")
                        {
                            UserInTopicInfo uintop = new UserInTopicInfo();
                            uintop.TopicID = setusersid;
                            uintop.UserID = int.Parse(setusersrej);
                            uintop.Remove();
                        }
                        SendToContact(int.Parse(setusersrej), string.Format("LEAVE:ID({0}),TYPE({1})>>", setusersid, actions["TYPE"][0]));
                    }
                    foreach (string setusersnew in actions["NEWUSERS"])
                    {
                        if (string.IsNullOrEmpty(setusersnew)) continue;
                        if (actions["TYPE"][0] == "topic")
                        {
                            UserInTopicInfo uintop = new UserInTopicInfo();
                            uintop.TopicID = setusersid;
                            uintop.UserID = int.Parse(setusersnew);
                            uintop.UserTopicRole = (actions["MENTORS"].Contains(setusersnew) ? UserTopicRoles.Mentor : UserTopicRoles.User);
                            uintop.Add();
                        }
                        SendToContact(int.Parse(setusersnew), string.Format("INVITED:ID({0}),TYPE({1})>>{2}", setusersid, actions["TYPE"][0], setusersdata));
                    }
                    TopicInfo.CheckForEmptyTopicByID(setusersid);
                    break;
                case "ATTACHMENT":
                    int attachmenttotopicid = int.Parse(actions["ID"][0]);
                    AttachmentInfo attach = new AttachmentInfo();
                    attach.AttachmentData = data;;
                    attach.AttachmentType = AttachmentTypes.Link;
                    attach.Update();
                    AttachmentInTopicInfo.Create(attachmenttotopicid, attach.AttachmentID).Add();
                    actions.Add("AID", new string[] { attach.AttachmentID.ToString() });
                    var newattachdata = combine();
                    UserInfo.GetByTopic(attachmenttotopicid).ForEach(delegate(UserInfo u) { SendToContact(u.UserID, newattachdata); });
                    break;
                case "ATTACHMENTGET":
                    AttachmentInfo.GetByTopic(int.Parse(actions["ID"][0])).ForEach(delegate(AttachmentInfo a) { user.Connection.Send("ATTACHMENT:ID(" + actions["ID"][0] + "),AID("+a.AttachmentID.ToString()+"),TYPE(link)>>" + a.AttachmentData); });
                    break;
                case "ATTACHMENTREMOVE":
                    attachmenttotopicid = int.Parse(actions["ID"][0]);
                    iid = int.Parse(actions["AID"][0]);
                    AttachmentInfo attmoveinfo = new AttachmentInfo();
                    attmoveinfo.AttachmentID = iid;
                    attmoveinfo.Remove();
                    UserInfo.GetByTopic(attachmenttotopicid).ForEach(delegate(UserInfo u) { SendToContact(u.UserID, e.Data); });
                    break;
            }
        }