Esempio n. 1
0
        public void JoinChat(WebClient client)
        {
            List <WebClient> clients;

            //If the chat is InviteOnly and the client is not invited and he is not the owner
            if (InviteOnly && client != m_Owner)
            {
                lock (m_Invited)
                {
                    //If he was not invited return
                    if (!m_Invited.Contains(client) && client != m_Owner)
                    {
                        return;
                    }
                }
            }

            //Ensure only 1 thread is in m_Present
            lock (m_Present)
            {
                //Ensure 1 thread in m_Banned
                lock (m_Banned)
                {
                    //If the client has already joined or he is banned we don't need to do anything else
                    if (m_Present.Contains(client) || m_Banned.Contains(client))
                    {
                        return;
                    }
                }
                //Get a list of clients to alert
                clients = m_Present.ToList();
                //Add the user to the present clients
                m_Present.Add(client);
            }

            //Create the message
            WebMessage message = CreateEnterExitMessage(client, false);

            //Indicate to all OTHER users that someone entered the chat
            clients.ForEach(c => c.Que(message));

            //Send the updated userList
            SendChatInfo();
        }
Esempio n. 2
0
        void ProcessInfo(Guid loginToken, HttpContextAdapter adapter)
        {
            //Construct a response.
            WebMessage message = new WebMessage("webServerInfo", ToJSON());

            //If we have a client then process the request as a message
            WebClient client;

            if (m_Clients.TryGetValue(loginToken, out client))
            {
                //Que the Information to the client
                client.Que(message);

                //End the request on the currentContext
                client.EndRequest(adapter);
            }
            else
            {
                //Write response
                WriteContextResponse(adapter, 200, "text/html", message.ToJSON(), Encoding);
                //WriteContextResponse(context, message.ToJSON(), "You have recieved the Server Information via JSON RPC, Please see the HEAD of this response!");
            }
        }
Esempio n. 3
0
 public void Que(WebMessage message)
 {
     Messages.Add(message);
 }
Esempio n. 4
0
 public void Send(WebMessage message)
 {
     Que(message);
     Release();
 }
Esempio n. 5
0
        internal void EndRequest(HttpContextAdapter adapter)
        {
            if (adapter == null)
            {
                return;
            }
            //Ensure there is only one thread working on this adapter
            lock (adapter)
            {
                //If there are messages to send, send them
                if (Messages.Count > 0)
                {
                    //Empty the volatile messages list into an array
                    WebMessage[] messages = Messages.ToArray();
                    Messages.Clear();

                    //Get the server encoding
                    Encoding encoding = Server.Encoding;

                    try
                    {
                        //FrameStart
                        adapter.OutputStream.WriteByte(encoding.GetBytes("[")[0]);
                    }
                    catch
                    {
                        //Reque the messages
                        Messages.AddRange(messages);
                        goto EndRequest;
                    }

                    byte[] binary;

                    //Get all of the messages except the last
                    if (messages.Length > 1)
                    {
                        //Iterate the array appending the JSON Object to the buffer
                        foreach (WebMessage message in messages.Take(Math.Max(1, messages.Length - 2)))
                        {
                            try
                            {
                                //Attempt to write the message
                                binary = encoding.GetBytes(message.ToJSON() + ',');
                                adapter.OutputStream.Write(binary, 0, binary.Length);
                            }
                            catch
                            {
                                //Reque the messages
                                Messages.Add(message);
                                continue;
                            }
                        }
                    }

                    //Get the last Message
                    WebMessage lastMessage = messages.Skip(messages.Length - 1).Take(1).Single();

                    try
                    {
                        //Attempt to write the lastMessage and FrameEnd
                        binary = encoding.GetBytes(lastMessage.ToJSON() + ']');
                        adapter.OutputStream.Write(binary, 0, binary.Length);
                    }
                    catch
                    {
                        //Reque the message
                        Messages.Add(lastMessage);
                        goto EndRequest;
                    }
                }

EndRequest:
                //End the current listen
                try
                {
                    adapter.OutputStream.Flush();
                    adapter.OutputStream.Close();
                    adapter.Close();
                    adapter.End();
                }
                catch
                {
                    return;
                }
            }
        }