Exemple #1
0
    void OnConnect(NetworkConnection c)
    {
        Debug.Log("Accepted a connection");

        // Example to send a handshake message:
        HandshakeMsg hsm = new HandshakeMsg();

        hsm.player.id = c.InternalId.ToString();


        // send all connection to client
        foreach (NetworkConnection con in m_Connections)
        {
            SendToClient(JsonUtility.ToJson(hsm), con);
        }

        ClientListMsg Clist = new ClientListMsg(ServerPlayersList);

        SendToClient(JsonUtility.ToJson(Clist), c);

        // send new player info
        hsm.cmd = Commands.PLAYER_JOIN;
        SendToClient(JsonUtility.ToJson(hsm), c);

        // add to server player list and connection list
        ServerPlayersList.Add(hsm.player);
        m_Connections.Add(c);


        print("User ID: " + hsm.player.id + " Connected!");
    }
Exemple #2
0
    // on data received
    void OnData(DataStreamReader stream)
    {
        NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp);

        stream.ReadBytes(bytes);
        string        recMsg = Encoding.ASCII.GetString(bytes.ToArray());
        NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg);

        switch (header.cmd)
        {
        // player handshake
        case Commands.HANDSHAKE:
            HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg);
            SpawnPlayer(hsMsg.player);
            Debug.Log("Handshake message received!");
            break;

        // player update
        case Commands.PLAYER_UPDATE:
            PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg);
            ClientPlayerUpdate(puMsg.players);
            Debug.Log("Player update message received!");
            break;

        // server update
        case Commands.SERVER_UPDATE:
            ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg);
            Debug.Log("Server update message received!");
            break;

        // player list update
        case Commands.PLAYER_LIST:
            ClientListMsg listMsg = JsonUtility.FromJson <ClientListMsg>(recMsg);
            for (int p = 0; p < listMsg.players.Length; p++)
            {
                SpawnPlayer(listMsg.players[p]);
            }
            Debug.Log("Player list message received!");
            break;

        // player disconnect
        case Commands.PLAYER_DISCONNECT:
            DiscPlayerMsg discMsg = JsonUtility.FromJson <DiscPlayerMsg>(recMsg);
            RemoveClientPlayer(discMsg.players);
            Debug.Log("Player disconnect message received!");
            break;

        // player connect
        case Commands.PLAYER_JOIN:
            HandshakeMsg nhsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg);
            ClientPlayerID = nhsMsg.player.id;
            SpawnPlayer(nhsMsg.player);
            break;

        default:
            Debug.Log("Unrecognized message received!");
            break;
        }
    }
Exemple #3
0
        static void SendClientList()
        {
            ClientListMsg clientListMsg = new ClientListMsg();

            lock (clientDictionary)
            {
                foreach (KeyValuePair <String, Socket> s in clientDictionary)
                {
                    clientListMsg.clientList.Add(s.Key);
                }

                MemoryStream outStream = clientListMsg.WriteData();

                foreach (KeyValuePair <String, Socket> s in clientDictionary)
                {
                    s.Value.Send(outStream.GetBuffer());
                }
            }
        }
Exemple #4
0
        private void SetClientList(ClientListMsg clientList)
        {
            if (this.InvokeRequired)
            {
                Invoke(new SetClientListDelegate(SetClientList), new object[] { clientList });
            }
            else
            {
                listBox_ClientList.DataSource = null;
                currentClientList.Clear();
                currentClientList.Add("All");

                foreach (String s in clientList.clientList)
                {
                    currentClientList.Add(s);
                }
                listBox_ClientList.DataSource = currentClientList;
            }
        }
Exemple #5
0
        // Send a message to every socket in the playerDictionary's PlayerInfo struct values, containing a current list of all client names from the keys of the dictionary
        static void SendClientList()
        {
            ClientListMsg clientListMsg = new ClientListMsg();

            // Lock
            lock (playerDictionary)
            {
                // Fill the clientListMsg with all the string dictionary keys
                foreach (KeyValuePair <String, PlayerInfo> pair in playerDictionary)
                {
                    clientListMsg.clientList.Add(pair.Key);
                }

                MemoryStream outStream = clientListMsg.WriteData();

                // Send it to each socket
                foreach (KeyValuePair <String, PlayerInfo> pair in playerDictionary)
                {
                    pair.Value.socket.Send(outStream.GetBuffer());
                }
            }
        }
    //Called on server when client disconnects
    public override void OnServerDisconnect(NetworkConnection conn)
    {
        base.OnServerDisconnect(conn);
        /*foreach (ClientContainer c in this.clients)
        {
            if(c.conn == conn)
            {
                GameObject.Find("Player List Container").GetComponent<Text>().text.Replace(c.username + "\n", "");
                Debug.Log(c.username + " disconnected.");
                this.clients.Remove(c);
                break;
            }
        }*/

        //Updating clients on change
        ClientListMsg updateMsg = new ClientListMsg();
        updateMsg.usernames = new string[this.clients.Count];
        for (int i = 0; i < this.clients.Count; i++)
        {
            updateMsg.usernames[i] = this.clients[i].username;
        }
        NetworkServer.SendToAll(ClientListMsg.msgType, updateMsg);
    }
        static void clientReceive(Object o)
        {
            Form1 form = (Form1)o;

            while (form.bConnected == true)
            {
                try
                {
                    byte[] buffer = new byte[4096];
                    int    result;

                    result = form.client.Receive(buffer);

                    if (result > 0)
                    {
                        MemoryStream stream = new MemoryStream(buffer);
                        BinaryReader read   = new BinaryReader(stream);

                        Msg m = Msg.DecodeStream(read);

                        if (m != null)
                        {
                            Console.Write("Got a message: ");
                            switch (m.mID)
                            {
                            case PublicChatMsg.ID:
                            {
                                PublicChatMsg publicMsg = (PublicChatMsg)m;

                                form.AddMessageText(publicMsg.msg);
                            }
                            break;

                            case PrivateChatMsg.ID:
                            {
                                PrivateChatMsg privateMsg = (PrivateChatMsg)m;
                                form.AddMessageText(privateMsg.msg);
                            }
                            break;

                            case ClientListMsg.ID:
                            {
                                ClientListMsg clientList = (ClientListMsg)m;
                                form.SetClientList(clientList);
                            }
                            break;

                            case ClientNameMsg.ID:
                            {
                                ClientNameMsg clientName = (ClientNameMsg)m;
                                form.SetClientName(clientName.name);
                            }
                            break;

                            case GameMsg.ID:
                            {
                                GameMsg gameMessage = (GameMsg)m;
                                form.AddGameText(gameMessage.msg);
                            }
                            break;



                            default:
                                break;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    form.bConnected = false;
                    Console.WriteLine("Lost server!");
                }
            }
        }
    void OnClientHandshakeReceived(NetworkMessage msg)
    {
        HandshakeMsg hmsg = msg.ReadMessage<HandshakeMsg>();
        ClientContainer c = new ClientContainer();
        c.username = hmsg.username;
        c.conn = msg.conn;
        this.clients.Add(c);

        Debug.Log(hmsg.username + " connected.");
        //Updating clients on change and telling newly connected of currently connected
        ClientListMsg updateMsg = new ClientListMsg();
        updateMsg.usernames = new string[this.clients.Count];
        for(int i = 0; i < this.clients.Count; i++)
        {
            updateMsg.usernames[i] = this.clients[i].username;
        }

        /*NetworkServer.SendToAll(ClientListMsg.msgType, updateMsg);
        hmsg.connectionID = msg.conn.connectionId;
        msg.conn.Send(HandshakeMsg.msgType, hmsg);*/

        foreach(PlayerController pc in GameObject.FindObjectsOfType<PlayerController>())
        {
            string username = null;
            for(int i = 0; i < this.clients.Count; i++)
            {
                if(this.clients[i].conn.connectionId == pc.playerID || (pc.connectionToClient == msg.conn && this.clients[i].conn == msg.conn))
                username = this.clients[i].username;
            }

            if(pc.connectionToClient == msg.conn)
                pc.RpcSetPlayerID(msg.conn.connectionId, username);
            else
                pc.RpcSetPlayerID(pc.playerID, username);
        }
    }