Hello everybody!
it’s my first post here, and i hope it’s not confusing or else…
I’m trying to build a “multiplayer” networking application, with a Unity server, some Unity Clients and Unity-WebGL Clients…
At first i wanted my “room-server” to be running on a WebGL, but i already found out that it must be on a Unity application…
So i tried to understand how the LLAPI works, and i found and followed a tutorial called
Unity Transport Layer (LLAPI) Tutorial (
)
which is composed by a server scene containing the server side, and a client scene containing a “player” gameobject and the client side…
I can get it working… well, the server starts (i start my server scene in the Unity application, and i’ve built a unity osx application for the client scene… i can get the server logging the player ConnectEvent and DisconnectEvent, but when it comes to DataEvent nothing happens…
I tried out other tutorials, and i always have issues with the DataEvent… may it be a NetworkTransport.Send error? i completely followed the tutorial and then i tried another one, but i always have issues with this…
I’m developing using Unity 2017.1 and OSX 10.9.5
Attaching the code below
Server side
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class UServer : MonoBehaviour {
int connectionID;
int maxConnections = 10;
int reliableChannelID;
int hostID;
int socketPort = 8888;
byte error;
public GameObject playerObject;
public Dictionary<int, GameObject> players = new Dictionary<int, GameObject> ();
// Use this for initialization
void Start () {
NetworkTransport.Init ();
ConnectionConfig serverConfig = new ConnectionConfig ();
reliableChannelID = serverConfig.AddChannel (QosType.ReliableSequenced);
HostTopology topology = new HostTopology (serverConfig, maxConnections);
hostID = NetworkTransport.AddHost (topology, socketPort, null);
Debug.Log("Socket open. Host ID is "+ hostID);
}
// Update is called once per frame
void Update () {
int recHostID;
int recConnectionID;
int recChannelID;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
NetworkEventType recNetworkEvent = NetworkTransport.Receive (out recHostID, out recConnectionID, out recChannelID, recBuffer, bufferSize, out dataSize, out error);
switch (recNetworkEvent) {
case NetworkEventType.ConnectEvent:
Debug.Log ("New player connected!");
GameObject newPlayer = Instantiate(playerObject, transform.position, transform.rotation);
players.Add(recConnectionID, newPlayer);
break;
case NetworkEventType.DataEvent:
string recMessage = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log ("Receiving: " + recMessage);
string[] message = recMessage.Split('|');
switch (message[0]) {
case "MOVE":
Move (message[1], message[2], players[recConnectionID]);
break;
}
break;
case NetworkEventType.DisconnectEvent:
Debug.Log ("Player disconnected!");
break;
case NetworkEventType.Nothing:
break;
}
}
void Move(string x, string y, GameObject obj) {
Debug.Log ("Moving the object!");
float xMov = float.Parse(x);
float yMov = float.Parse(y);
obj.transform.Translate(xMov, 0f, yMov);
}
}
Client side
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class UClient : MonoBehaviour {
int connectionID;
int maxConnections = 10;
int reliableChannelID;
int hostID;
int socketPort = 8888;
byte error;
private bool connected;
// Use this for initialization
void Start () {
NetworkTransport.Init ();
connected = false;
}
// Update is called once per frame
void Update () {
if (!connected) {
if (Input.GetKeyDown(KeyCode.C))
{
Connect();
connected = true;
}
}
}
void OnGUI() {
if (!connected){
GUI.Label(new Rect(2, 50, 150, 100), "Press C to connect");
}
}
public void Connect () {
ConnectionConfig serverConfig = new ConnectionConfig ();
reliableChannelID = serverConfig.AddChannel(QosType.ReliableSequenced);
HostTopology topology = new HostTopology (serverConfig, maxConnections);
hostID = NetworkTransport.AddHost (topology, 0);
Debug.Log("Socket open. Host ID is "+ hostID);
connectionID = NetworkTransport.Connect(hostID, "127.0.0.1", socketPort, 0, out error);
}
public void Disconnect() {
NetworkTransport.Disconnect (hostID, connectionID, out error);
}
public void sendMessage(string message) {
Debug.Log ("Sending message: " + message);
byte[] msgBuffer = Encoding.Unicode.GetBytes(message);
int bufferSize = message.Length * sizeof(char);
NetworkTransport.Send(hostID, connectionID, reliableChannelID, msgBuffer, bufferSize, out error);
}
}
Player script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UPlayer : MonoBehaviour {
Rigidbody body;
public UClient client;
// Use this for initialization
void Start () {
body = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
float xMov = Input.GetAxis ("Horizontal");
float yMov = Input.GetAxis ("Vertical");
transform.Translate (xMov, 0, yMov);
if (xMov != 0 || yMov != 0) {
string msg = "MOVE|" + xMov.ToString () + "|" + yMov.ToString ();
client.sendMessage (msg);
}
}
}
thank you very much for any help or tip!!