Ejemplo n.º 1
0
        public void MessageReceived(WsConnection sender,
                                    MessageReceivedEventArgs e)
        {
            if (e.Message.Length >= 1)
            {
                if (e.Message[0] == '\u0003')
                {
                    if (AgentDisconnected != null)
                    {
                        AgentDisconnected(this, EventArgs.Empty);
                    }
                }

                else if (e.Message.Length > 1)
                {
                    string s = e.Message.Substring(0, 1);
                    int    i = 0;
                    if (int.TryParse(s, out i))
                    {
                        WsClientConnection client = _clients[i];
                        if (client != null)
                        {
                            client.SendMessage(e.Message.Substring(2));
                        }
                    }
                }
            }
        }
        void MessageReceived(WsConnection sender, MessageReceivedEventArgs e)
        {
            string msg = e.Message;

            log.Debug("Message received: " + msg);

            if (e.DataLength > 14 && (msg.Substring(0, 14) == "[Agent SignOn:"))
            {
                // This is an agent signing on
                string            name  = msg.Substring(14, e.DataLength - 15);
                WsAgentConnection agent = new WsAgentConnection(sender, name);

                // Re-wire the event handlers
                sender.Disconnected    -= Disconnected;
                sender.MessageReceived -= MessageReceived;
                sender.Disconnected    += agent.Disconnected;
                sender.MessageReceived += agent.MessageReceived;

                agent.AgentDisconnected +=
                    new WsDisconnectedAgentEventHandler(AgentDisconnected);

                // Move this socket to the agent list
                _unknown.Remove(sender);
                _agents.Add(agent);

                log.InfoFormat("Socket attached to agent {0}, handle = {1}",
                               name, sender._mySocket.Handle.ToString());

                // Send a response
                agent.SendMessage("Welcome, " + name);
            }
            else if (e.DataLength > 15 &&
                     (msg.Substring(0, 15) == "[Client SignOn:"))
            {
                // This is a client requesting assistance
                string name = msg.Substring(15, e.DataLength - 16);

                // Find an agent
                WsAgentConnection agent = null;
                int clientID            = 0;
                foreach (WsAgentConnection a in _agents)
                {
                    foreach (KeyValuePair <int, WsClientConnection> d in a._clients)
                    {
                        if (d.Value == null)
                        {
                            agent    = a;
                            clientID = d.Key;
                            break;
                        }
                    }
                    if (agent != null)
                    {
                        break;
                    }
                }

                if (agent != null)
                {
                    WsClientConnection client =
                        new WsClientConnection(sender, agent, clientID, name);

                    log.InfoFormat("Client {0} assigned to agent {1}", name, agent._name);

                    // Re-wire the event handlers
                    sender.Disconnected    -= Disconnected;
                    sender.MessageReceived -= MessageReceived;
                    sender.Disconnected    += client.Disconnected;
                    sender.MessageReceived += client.MessageReceived;

                    client.ClientDisconnected +=
                        new WsDisconnectedClientEventHandler(ClientDisconnected);

                    // Add this to the agent list
                    _unknown.Remove(sender);
                    _clients.Add(client);

                    agent._clients[clientID] = client;

                    // Send a message to the agent
                    agent.SendMessage("[ClientName:" + clientID.ToString() +
                                      name + "]");

                    // Send a response
                    client.SendMessage("Hello! My name is " + agent._name +
                                       ". How may I help you?");
                }
                else
                {
                    // There are no agents available
                    sender.SendMessage("There are no agents currently available;" +
                                       "please try again later");

                    sender.Dispose();
                }
            }
        }