- Home /
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
Answer by cskiwi · Jun 14, 2012 at 02:18 PM
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
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!
You might not see this but thanks! Ive been playing about with UDP and multicasting trying to get the same message sent to two different clients but to no avail, this approach works like a charm :)
@cskiwi : Since I am new to networking,I don't know whether the doubt I am asking is correct or logically sounds good. I need to know whether the above client and server script is using TCP socket program$$anonymous$$g?
@ash_05 : This was some code from 4 years ago, haven't programmed a lot in Unity ever since, however; for your question, I'm not sure what you wanna know. I can tell you that I didn't test if the RPC works with UDP or TCP, but I would suspect UDP (because as server we don't care if the user receives the package or not)
Your answer
Follow this Question
Related Questions
Sending local configuration data from Client to the Server 1 Answer
Server not receiving message? 1 Answer
Unity networking tutorial? 6 Answers
NetworkMessages in UNET 0 Answers