x


Networking: sendinge message from server to client(s)

hi

I'm currently trying to send a message from my server to 1 or more clients

the server is running an different exe then the clients. But how do you do this?

Does I need to make an networkview? I realy don't know how I should start on this

more ▼

asked Jun 13 '12 at 10:25 PM

cskiwi gravatar image

cskiwi
0 1 1 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Got it up and running ;) I needed to have the rpc message in both projects :)

here are the 2 scripts that i used:

SERVER:

using UnityEngine;
using System.Collections;

public class ServerNetwork : MonoBehaviour {
    private int port = 25000;
    private int playerCount = 0;
    private string _messageLog = "";

    public void Awake() {
        if (Network.peerType == NetworkPeerType.Disconnected)
            Network.InitializeServer(10, port, false);
    }

    public void Update() {

    }
    public void OnGUI() {
        if (Network.peerType == NetworkPeerType.Server) {
            GUI.Label(new Rect(100, 100, 150, 25), "Server");
            GUI.Label(new Rect(100, 125, 150, 25), "Clients attached: " + Network.connections.Length);

            if (GUI.Button(new Rect(100, 150, 150, 25), "Quit server")) {
                Network.Disconnect();
                Application.Quit();
            }
            if (GUI.Button(new Rect(100, 175, 150, 25), "Send hi to client"))
                SendInfoToClient();
        }
        GUI.TextArea(new Rect(275, 100, 300, 300), _messageLog);
    }

    void OnPlayerConnected(NetworkPlayer player) {
        AskClientForInfo(player);
    }

    void AskClientForInfo(NetworkPlayer player) {
        networkView.RPC("SetPlayerInfo", player, player);
    }

    [RPC]
    void ReceiveInfoFromClient(string someInfo) {
        _messageLog += someInfo + "\n";
    }

    string someInfo = "Server: hello client";
    [RPC]
    void SendInfoToClient() {
        networkView.RPC("ReceiveInfoFromServer", RPCMode.Others, someInfo);
    }

    // fix RPC errors
    [RPC]
    void SendInfoToServer() { }
    [RPC]
    void SetPlayerInfo(NetworkPlayer player) { }
    [RPC]
    void ReceiveInfoFromServer(string someInfo) { }
}

CLIENT:

using UnityEngine;
using System.Collections;

public class ClientNetwork : MonoBehaviour {

    public string serverIP = "127.0.0.1";
    public int port = 25000;
    private string _messageLog = "";
    string someInfo = "";
    private NetworkPlayer _myNetworkPlayer;

    void OnGUI() {
        if (Network.peerType == NetworkPeerType.Disconnected) {
            if (GUI.Button(new Rect(100, 100, 150, 25), "Connect")) {
                Network.Connect(serverIP, port);
            }
        } else {
            if (Network.peerType == NetworkPeerType.Client) {
                GUI.Label(new Rect(100, 100, 150, 25), "client");

                if (GUI.Button(new Rect(100, 125, 150, 25), "Logut"))
                    Network.Disconnect();

                if (GUI.Button(new Rect(100, 150, 150, 25), "SendHello to server"))
                    SendInfoToServer();
            }
        }

        GUI.TextArea(new Rect(250, 100, 300, 300), _messageLog);
    }

    [RPC]
    void SendInfoToServer(){
        someInfo = "Client " + _myNetworkPlayer.guid + ": hello server";
        networkView.RPC("ReceiveInfoFromClient", RPCMode.Server, someInfo);
    }
    [RPC]
    void SetPlayerInfo(NetworkPlayer player) {
        _myNetworkPlayer = player;
        someInfo = "Player setted";
        networkView.RPC("ReceiveInfoFromClient", RPCMode.Server, someInfo);
    }

    [RPC]
    void ReceiveInfoFromServer(string someInfo) {
        _messageLog += someInfo + "\n";
    }

    void OnConnectedToServer() {
        _messageLog += "Connected to server" + "\n";
    }
    void OnDisconnectedToServer() {
        _messageLog += "Disco from server" + "\n";
    }

    // fix RPC errors
    [RPC]
    void ReceiveInfoFromClient(string someInfo) { }
    [RPC]
    void SendInfoToClient(string someInfo) { }
}

I hope it is to good use for somebody, if there are any questions about this, or if you see that I made a mistake, let me know

ps: I didn't organize realy these scripts, it was just for testing and getting it to work grt Kiwi

more ▼

answered Jun 14 '12 at 02:18 PM

cskiwi gravatar image

cskiwi
0 1 1 2

It would be really great if you could post the answer here by editing your answer above. UA is the knowledge base system and it's good to have the answers here :) Then mark it as answered!

Jun 14 '12 at 02:19 PM whydoidoit

Here you go, inserted the code

Jun 14 '12 at 02:55 PM cskiwi
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x725
x15

asked: Jun 13 '12 at 10:25 PM

Seen: 841 times

Last Updated: Jun 14 '12 at 03:08 PM