• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mark-pollard · Oct 22, 2016 at 08:56 AM · networkingserverclient

How to send a message to the server

HI,

I want to have some clients and a server all running android. I want to connect all the clients to the server. The clients will have a timer running on them, the servers job will be to show the status of what the clients are doing. I want to be able to send messages from the clients to the server about what it is doing. Is there a way to do this ?

Here is what i have so far but i am unable to catch the message on the server object.

Here is the clients code

using UnityEngine; using System.Collections; using UnityEngine; using UnityEngine.Networking;

class RegisterHostMessage : MessageBase { public float message; }

public class Client : MonoBehaviour {

 NetworkClient myclient;
 public UnityEngine.UI.Text conText;
 public static short clientID = 123;

 // Use this for initialization
 void Start () {
     SetUpclient ();
 }
 
 // Update is called once per frame
 void SetUpclient () {
     myclient = new NetworkClient ();
     myclient.RegisterHandler (MsgType.Connect, OnConnected);
     myclient.Connect ("10.244.144.201", 4444);

 }

 public void OnConnected(NetworkMessage netMsg)
 {
     RegisterHostMessage msg = new RegisterHostMessage ();
     msg.message = 12.45f;
     conText.text = "Connected to server";

     myclient.Send (clientID,msg);
 }

}

Here is the server's code.

using UnityEngine; using System.Collections; using UnityEngine; using UnityEngine.Networking;

public class Master : MonoBehaviour {

 public UnityEngine.UI.Text masterip;

 // Use this for initialization
 void Start () {
     SetUpServer ();
 }

 void SetUpServer()
 {
     NetworkServer.Listen (4444);
     masterip.text = Network.player.ipAddress;
 
 }


 public void OnCommandSent(NetworkMessage netMsg)
 {
     Debug.Log (netMsg);


 }

}

Any help will be good.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mark-pollard · Oct 27, 2016 at 06:56 AM 0
Share

Anybody got any ideas?

avatar image ForeignGod · Oct 27, 2016 at 12:20 PM 0
Share

Try changing monobehaviour to NetworkBehaviour and send messages to the server by using commands. https://docs.unity3d.com/ScriptReference/Networking.CommandAttribute.html https://docs.unity3d.com/$$anonymous$$anual/UNetActions.html

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by RodrigoAbreu · Nov 05, 2016 at 09:21 PM

Hi Mark, on the method SetUpServer, before NetworkServer.Listen (4444);, put: NetworkServer.RegisterHandler(123, OnCommandSent);

to define the Register para receber a mensagem recebida para o ID 123.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image atomjackJohn · Jan 03, 2020 at 01:01 AM 0
Share

Thanks for this. I was running into a similar problem (registering handlers on my clients), but didn't remember to register the handler on my server!

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

Using smartphone as controller. How do I send/receive button states? 0 Answers

Learning Project: How is Badumna for a server? 1 Answer

connecting two people over the Internet 1 Answer

Server not receiving message? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges