Unity2D: UNET transport layer API server script isn't messaging properly - unity3d

I'm following a tutorial unity multiplayer (UNET) on YouTube but I'm getting different results compared to the guy in the video. You see I followed his tutorial to the tea, however in my server script within the update function I used a debug.log in the switch statement default function to tell me if i'm receiving invalid messaging, which I am. I think that my message isn't really being sent properly despite the fact that I followed the guy's tutorial carefully! Am I missing something in my server script?
This is my code:
public class ServerClient
{
public int connectionId;
public string playerName;
}
public class Server : MonoBehaviour {
private const int MAX_CONNECTION = 4;
private int port = 8888;
private int hostId;
private int webHostId;
private int reliableChannel;
private int unreliableChannel;
private bool isStarted = false;
private byte error;
private List<ServerClient> clients = new List<ServerClient>();
private void Start()
{
NetworkTransport.Init ();
ConnectionConfig cc = new ConnectionConfig ();
reliableChannel = cc.AddChannel (QosType.Reliable);
unreliableChannel = cc.AddChannel (QosType.Unreliable);
HostTopology topo = new HostTopology (cc, MAX_CONNECTION);
hostId = NetworkTransport.AddHost (topo, port); // null
Debug.Log ("Socket Open. hostId is: " + hostId);
webHostId = NetworkTransport.AddWebsocketHost (topo, port, null); //, port, null
isStarted = true;
}
private void Update()
{
if (!isStarted)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive (out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData) {
case NetworkEventType.ConnectEvent: //2
Debug.Log ("Player " + connectionId + "has connected");
OnConnection (connectionId);
break;
case NetworkEventType.DataEvent: //3
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
//Debug.Log("Player" + connectionId + " has sent : " + msg);
Debug.Log("Recieving from " + connectionId + " : " + msg);
string[] splitData = msg.Split ('|');
switch (splitData[0])
{
case "OnNameIs":
OnNameIs (connectionId, splitData [1]);
break;
default:
Debug.Log ("Invalid message: " + msg);
break;
}
break;
case NetworkEventType.DisconnectEvent: // 4
Debug.Log("Player " + connectionId + "has disconnected");
break;
}
}
private void OnConnection(int cnnId)
{
// This may add a thrid player
ServerClient c = new ServerClient();
c.connectionId = cnnId;
c.playerName = "TEMP";
clients.Add(c);
//So you might want to change this later
string msg = "AskName|" + cnnId + "|";
foreach (ServerClient sc in clients)
msg += sc.playerName + '%' + sc.connectionId + '|';
msg = msg.Trim ('|');
Send (msg, reliableChannel, cnnId);
}
private void OnNameIs(int cnnId, string playerName)
{
clients.Find (x => x.connectionId == cnnId).playerName = playerName;
Send ("CNN|" + playerName + '|' + cnnId, reliableChannel, clients);
}
private void Send(string message, int channelId, int cnnId)
{
List<ServerClient> c = new List<ServerClient>();
c.Add (clients.Find (x => x.connectionId == cnnId));
Send (message, channelId, c);
}
private void Send(string message, int channelId, List<ServerClient> c)
{
Debug.Log ("Sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes (message);
foreach (ServerClient sc in c)
{
NetworkTransport.Send (hostId, sc.connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
}
}
The below script is my client script:
public class Player
{
public string playerName;
public GameObject avatar;
public int connectionId;
}
public class Client : MonoBehaviour {
private const int MAX_CONNECTION = 4;
private int port = 8888; //5701
private int hostId;
private int webHostId;
private int connectionId;
private int ourClientId;
private int reliableChannel;
private int unreliableChannel;
private float connectionTime;
private bool isConnected = false;
private bool isStarted = false;
private byte error;
private string playerName;
public GameObject playerPrefab;
public List<Player> players = new List<Player>();
public void Connect()
{
//Does the player have a name?
//take this part out
string pName = GameObject.Find("NameInput").GetComponent<InputField>().text;
if (pName == "") {
Debug.Log ("you must enter a name");
return;
}
playerName = pName;
//
// place this is the Start fuction
NetworkTransport.Init ();
ConnectionConfig cc = new ConnectionConfig ();
reliableChannel = cc.AddChannel (QosType.Reliable);
unreliableChannel = cc.AddChannel (QosType.Unreliable);
HostTopology topo = new HostTopology (cc, MAX_CONNECTION);
hostId = NetworkTransport.AddHost (topo, 0);
//
byte error;
connectionId = NetworkTransport.Connect (hostId, "127.0.0.1", port, 0, out error);
Debug.Log ("Connection to server. ConnectionId: " + connectionId);
connectionTime = Time.time;
isConnected = true;
}
private void Update()
{
if (!isConnected)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive (out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData) {
case NetworkEventType.DataEvent: //1
string msg = Encoding.Unicode.GetString (recBuffer, 0, dataSize);
Debug.Log ("Recieving : " + msg);
string[] splitData = msg.Split ('|');
switch (splitData[0])
{
case "AskName":
OnAskName (splitData);
break;
case "CNN":
SpawnPlayer (splitData[1], int.Parse(splitData[2]));
break;
case "DC":
break;
default:
Debug.Log ("Invalid message: " + msg);
break;
}
break;
}
}
private void OnAskName(string[] data)
{
ourClientId = int.Parse (data [1]);
Send ("OnNameis|" + playerName, reliableChannel);
for (int i = 2; i < data.Length - 1; i++)
{
string[] d = data[i].Split('%');
SpawnPlayer(d[0], int.Parse(d[1]));
}
}
private void SpawnPlayer(string playerName, int cnnId)
{
GameObject go = Instantiate (playerPrefab) as GameObject;
Debug.Log ("Object has been spawn", go);
if (cnnId == ourClientId) // the problem //
{
GameObject.Find("Canvas").SetActive (false);
isStarted = true;
}
Player p = new Player ();
p.avatar = go;
p.playerName = playerName;
p.connectionId = cnnId;
p.avatar.GetComponentInChildren<TextMesh>().text = playerName;
players.Add (p);
}
private void Send(string message, int channelId)
{
Debug.Log ("Sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes (message);
NetworkTransport.Send (hostId, connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
Thank you in advance!

Not quite to the tea.
If you look closely you'll see that:
The client sends a command with the name OnNameis
While the server expects a command named OnNameIs
This pretty much illustrates the dangers of using strings for commands. Use an Enum instead.

Related

Multi-Client Chat and file transfer in linux

\I want to make a socket application that will run on Linux and provide multiple messaging and file transfer. Can you share sample code? Can you help me i am a second year computer engineering student?
I found a similar code in my research, but I cannot connect to my linux server from different computers.I learned this code from github #HondaPL.
This is client codes
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Collections.Generic;
using MessengerLogic;
using System.IO;
namespace Messenger
{
public class Connection
{
public static int BufferSize = 1024;
public static string nick;
public static IPAddress ip = IPAddress.Parse("-.-.-.-");
public static int port = 1234;
public static TcpClient client = new TcpClient();
public static List<string> users = new List<string>();
public static bool isEnd = false;
public static string[] fromServer = new string[30];
public void Connect(string nickname)
{
nick = nickname;
client.Connect(ip, port);
nickname += "$Welcome$";
byte[] buffer = Encoding.ASCII.GetBytes(nickname);
NetworkStream ns = client.GetStream();
Console.WriteLine(nickname);
ns.Write(buffer, 0, buffer.Length);
Thread thread = new Thread(o => ReceiveData((TcpClient)o));
thread.Start(client);
}
static void ReceiveData(TcpClient client)
{
NetworkStream ns = client.GetStream();
byte[] receivedBytes = new byte[1024];
int byte_count;
StringParser parser = new StringParser();
while ((byte_count = ns.Read(receivedBytes, 0, receivedBytes.Length)) > 0)
{
fromServer = new string[30];
users.Clear();
string message = Encoding.ASCII.GetString(receivedBytes, 0, byte_count);
fromServer = parser.Parser(message);
if (fromServer[0] == "List")
{
for (int i = 1; i < fromServer.Length; i++)
{
users.Add(fromServer[i]);
}
isEnd = true;
}
else
{
Console.WriteLine(message);
Console.WriteLine(fromServer[0]);
Console.WriteLine(fromServer[1]);
FrmOnlineUsers.frm.AssignValues(fromServer[0], fromServer[1]);
FrmOnlineUsers.frm.MessageFromClient();
}
}
}
public List<string> Refresh()
{
NetworkStream ns = client.GetStream();
string temp = nick;
temp += "$List$";
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(temp);
ns.Write(buffer, 0, buffer.Length);
while (!isEnd)
{
}
isEnd = false;
return users;
}
public void SendMessage(string who, string msg)
{
NetworkStream ns = client.GetStream();
string temp = $"{who}$Message${msg}$";
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(temp);
ns.Write(buffer, 0, buffer.Length);
}
public void FileSend(string filepath, string nick, string fileName)
{
NetworkStream ns = client.GetStream();
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
byte[] bytesToSend = new byte[fs.Length];
int numBytesRead = fs.Read(bytesToSend, 0, bytesToSend.Length);
int totalBytes = 0;
//Tutaj trzeba dodać, aby pobierał nazwę pliku
string temp = $"{nick}$File${fileName}${fs.Length}";
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(temp);
ns.Write(buffer, 0, buffer.Length);
Thread.Sleep(1000);
for (int i = 0; i <= fs.Length / BufferSize; i++)
{
if (fs.Length - (i * BufferSize) > BufferSize)
{
ns.Write(bytesToSend, i * BufferSize,
BufferSize);
totalBytes += BufferSize;
}
else
{
ns.Write(bytesToSend, i * BufferSize,
(int)fs.Length - (i * BufferSize));
totalBytes += (int)fs.Length - (i * BufferSize);
}
}
fs.Close();
}
}
}

Both client and server windows are blank

I am trying to test player position communication between a client and a server. I am doing this by building a game environment where the other player's position is also displayed. They connect properly, but both the windows are blank white. If I terminate one program only then the other displays the playground. Also in the action listener which listens for the timer only sends the data and doesn't proceed. Can I get help for displaying the windows properly and fixing the action listener?
Here is the client code :
public class SamplePlayerClient1
{
private static JFrame window = new JFrame("Sample Player Client 1");
private static class Ground extends JPanel {
private static final long serialVersionUID = 1L;
private int px = 50, py = 235, ex, ey, key;
private DataWriter writer;
private DataReader reader;
private ActionListener timerListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
writer.send(px, py);
System.out.println("data sent");
ex = reader.read().x;
ey = reader.read().y;
System.out.println("Data read");
} catch (IOException e1) {
e1.printStackTrace();
}
repaint();
}
};
private Timer animator = new Timer(30, timerListener);
private KeyAdapter keyListener = new KeyAdapter() {
public void keyPressed(KeyEvent e) {
key = e.getKeyCode();
if(key == KeyEvent.VK_UP) {
py -= 8;
if(py < 0) {
py = 0;
}
} else if(key == KeyEvent.VK_DOWN) {
py += 8;
if(py > 430) {
py = 430;
}
} else if(key == KeyEvent.VK_LEFT) {
px -= 8;
if(px < 0) {
px = 0;
}
} else if(key == KeyEvent.VK_RIGHT) {
px += 8;
if(px > 455) {
px = 455;
}
}
}
};
Ground() throws UnknownHostException, IOException {
Socket soc = new Socket(InetAddress.getByName("192.168.0.3"), 9998);
System.out.println("client 2 connected");
writer = new DataWriter(soc);
reader = new DataReader(soc);
animator.start();
addKeyListener(keyListener);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
requestFocus();
g.setColor(Color.BLACK);
g.fillRect(px, py, 30, 30);
g.setColor(Color.RED);
g.fillRect(ex, ey, 30, 30);
}
}
private static class DataWriter {
private OutputStreamWriter writer;
private String point = "";
DataWriter(Socket soc) throws IOException {
writer = new OutputStreamWriter(soc.getOutputStream());
}
void send(int x, int y) throws IOException {
point = "" + x;
point += ",";
point += y;
point += "\n";
writer.write(point);
writer.flush();
}
}
private static class DataReader {
private InputStreamReader is;
private BufferedReader reader;
private String point = "";
DataReader(Socket soc) throws IOException {
is = new InputStreamReader(soc.getInputStream());
reader = new BufferedReader(is);
}
Point read() throws IOException {
point = reader.readLine();
int x = Integer.parseInt(point.split(",")[0]);
int y = Integer.parseInt(point.split(",")[1]);
return new Point(x, y);
}
}
public static void main(String [] args) throws UnknownHostException, IOException {
Ground ground = new Ground();
window.setContentPane(ground);
window.setSize(600, 600);
window.setLocation(0, 0);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
I don't see the problem but I did see that:
ex = reader.read().x;
ey = reader.read().y;
This reads two lines and throws away the y from the first and the x from the second.
Should be:
Point p = reader.read();
ex = p.x;
ey = p.y;
The peer might not generate a point for you to read every time you write one so the reading of the points from the peer should not occur in the event handler that writes a point to the peer.

Unity - UNET gameobject and child rotation

I've been trying for hours to fix this but now I have no idea what's wrong. Here is a video: https://www.youtube.com/watch?v=X8JrXwgvvFg As you can see my player is rotating and it's synced (as you can see by text above player), but the "hand" isn't and I don't know why. Can you help me?
Client.cs:
using System.Collections;
using System.Collections.Generic;
using System.Text;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using UnityStandardAssets.Characters.FirstPerson;
public class Player
{
public string playerName;
public GameObject avatar;
public int connectionID;
}
public class ClientManager : MonoBehaviour {
private const int MAX_PLAYERS = 24;
private int port = 5701;
private int hostId;
private int reliableChannel;
private int unreliableChannel;
private int clientID;
private int connectionId;
private float connectionTime;
private bool isConnected = false;
private bool isStarted = false;
private byte error;
private string playerName;
public GameObject playerPrefab;
public Dictionary<int, Player> players = new Dictionary<int, Player>();
public void Connect()
{
string pName = GameObject.Find("NameInput").GetComponent<InputField>().text;
if (pName == "")
{
Debug.LogError("You must type in your nickname before you connect!");
return;
}
playerName = pName;
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_PLAYERS);
hostId = NetworkTransport.AddHost(topo, 0);
connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);
connectionTime = Time.time;
isConnected = true;
}
private void Update()
{
if (!isConnected)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("Receiving: " + msg);
string[] splitdata = msg.Split('|');
switch (splitdata[0])
{
case "ASKNAME":
OnAskName(splitdata);
break;
case "CNN":
Debug.LogError(splitdata[1] + " connected!");
SpawnPlayer(splitdata[1], int.Parse(splitdata[2]));
break;
case "DC":
Debug.LogError(splitdata[1] + " disconnected!");
PlayerDisconnected(int.Parse(splitdata[2]));
break;
case "ASKPOSITION":
OnAskPosition(splitdata);
break;
default:
Debug.LogError("Wrong message: " + msg);
break;
}
break;
}
}
private void OnAskPosition(string[] data)
{
if (!isStarted)
return;
//Update everyone else
for (int i = 1; i < data.Length; i++)
{
string[] d = data[i].Split('%');
//Prevent to server from updating us
if (clientID != int.Parse(d[0]))
{
Vector3 position = Vector3.zero;
position.x = float.Parse(d[1]);
position.y = float.Parse(d[2]);
position.z = float.Parse(d[3]);
players[int.Parse(d[0])].avatar.transform.position = position;
players[int.Parse(d[0])].avatar.transform.rotation = Quaternion.Euler(0, float.Parse(d[4]), 0);
players[int.Parse(d[0])].avatar.transform.GetChild(0).rotation = Quaternion.Euler(float.Parse(d[5]), 0, 0);
Debug.LogWarning("THEIR Player rot: " + float.Parse(d[4]));
Debug.LogWarning("THEIR Camera rot: " + float.Parse(d[5]));
}
}
//Send our own position
Vector3 myPosition = players[clientID].avatar.transform.position;
Debug.LogWarning("MY Player rot Y: " + players[clientID].avatar.transform.rotation.eulerAngles.y.ToString());
Debug.LogWarning("MY Cam rot X: " + players[clientID].avatar.transform.GetChild(0).rotation.eulerAngles.x.ToString());
string m = "MYPOSITION|" + myPosition.x.ToString() + '|' + myPosition.y.ToString() + '|' + myPosition.z.ToString() + '|' + players[clientID].avatar.transform.rotation.eulerAngles.y.ToString() + '|' + players[clientID].avatar.transform.GetChild(0).rotation.eulerAngles.x.ToString();
Send(m, unreliableChannel);
}
Server.cs
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class Players
{
public int connectionId;
public string playerName;
public Vector3 position;
public float rotationPlayerY;
public float rotationCameraX;
}
public class ServerManager : MonoBehaviour
{
private const int MAX_PLAYERS = 24;
private int port = 5701;
private int hostId;
private int reliableChannel;
private int unreliableChannel;
private bool isStarted = false;
private byte error;
private List<Players> players = new List<Players>();
private float lastmovementUpdate;
private float movementUpdateRate = 0.05f;
private void Start()
{
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_PLAYERS);
hostId = NetworkTransport.AddHost(topo, port, null);
isStarted = true;
}
private void Update()
{
if (!isStarted)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.ConnectEvent:
Debug.Log("Player " + connectionId + " connected!");
PlayerConnect(connectionId);
break;
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("Receiving from " + connectionId + ": " + msg);
string[] splitdata = msg.Split('|');
switch (splitdata[0])
{
case "NAMEIS":
OnNameIs(connectionId, splitdata[1]);
break;
case "MYPOSITION":
OnMyPosition(connectionId, float.Parse(splitdata[1]), float.Parse(splitdata[2]), float.Parse(splitdata[3]));
OnMyRotation(connectionId, float.Parse(splitdata[4]), float.Parse(splitdata[5]));
break;
default:
Debug.LogError("Wrong message: " + msg);
break;
}
break;
case NetworkEventType.DisconnectEvent:
Debug.Log("Player " + connectionId + " disconnected!");
OnDisconnection(connectionId);
break;
}
//ask player for their position
if(Time.time - lastmovementUpdate > movementUpdateRate && players.Count > 0)
{
lastmovementUpdate = Time.time;
string m = "ASKPOSITION|";
foreach (Players pl in players)
{
//POSITION UPDATE
m += pl.connectionId.ToString() + '%' + pl.position.x.ToString() + '%' + pl.position.y.ToString() + '%' + pl.position.z.ToString() + '%' + pl.rotationPlayerY.ToString() + '%' + pl.rotationCameraX.ToString() + '|';
}
m = m.Trim('|');
Send(m, unreliableChannel, players);
}
}
private void OnMyPosition(int connID, float x, float y, float z)
{
players.Find(q => q.connectionId == connID).position = new Vector3(x, y, z);
}
private void OnMyRotation(int connID, float playerRot, float cameraRot)
{
players.Find(q => q.connectionId == connID).rotationCameraX = cameraRot;
players.Find(q => q.connectionId == connID).rotationPlayerY = playerRot;
}

WebGL and UNet. Cant seem to get in proper contact with server from a browser

I am trying to setup a multiplayer game to be played through a browser using UNet. In the editor everything works fine, I followed a tutorial (https://www.youtube.com/watch?v=qGkkaNkq8co) that said it should work in webgl but sadly it does not. There are two scripts as of yet one for the client and one for the server linked below. This is using mostly the LLAPI.
Whether I use the networkServer.usewebsocket = true or not, the result is the same.
The debug of "finished connection" does show so it completes the OnConnection function.
From the editor everything works fine but from the browser I get the following error:
"Attempt to send to not connected connection {1}" -- Since I get this error when ASKNAME is asked and when ASKPOSITION is asked I suspect that the SEND function is the problem im trying to send something the browser cant understand. But I don't know why.
private int port = 3001;
private List<ServerClient> clients = new List<ServerClient>();
private float lastMovementUpdate;
private float movementUpdateRate = 0.05f;
private int socketId;
private int webHostId;
private int reliableChannel;
private int unReliableChannel;
private bool isStarted = false;
private byte error;
private void Start()
{
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unReliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_CONNECTION);
socketId = NetworkTransport.AddHost(topo, port, null);
webHostId = NetworkTransport.AddWebsocketHost(topo, port, null);
isStarted = true;
}
private void Update()
{
if (!isStarted)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.Nothing: //1
break;
case NetworkEventType.ConnectEvent: //2
Debug.Log("Player " + connectionId + "has connected");
OnConnection(connectionId);
break;
case NetworkEventType.DataEvent: //3
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("Receiving From " + connectionId + "has sent : " + msg);
string[] splitData = msg.Split('|');
switch (splitData[0])
{
case "NAMEIS":
OnNameIS(connectionId, splitData[1]);
break;
case "MYPOSITION":
OnMyPosition(connectionId, float.Parse(splitData[1]), float.Parse(splitData[2]));
break;
default:
Debug.Log("invalid message : " + msg);
break;
}
break;
case NetworkEventType.DisconnectEvent: //4
Debug.Log("Player " + connectionId + "has disconnected");
OnDiconnection(connectionId);
break;
}
if (clients.Count > 0)
{
if (Time.time - lastMovementUpdate > movementUpdateRate)
{
lastMovementUpdate = Time.time;
string posMsg = "ASKPOSITION|";
foreach (ServerClient sc in clients)
posMsg += sc.connectionId.ToString() + '%' + sc.position.x.ToString() + '%' + sc.position.y.ToString() + '|';
posMsg = posMsg.Trim('|');
Send(posMsg, unReliableChannel, clients);
}
}
}
private void OnConnection(int cnnId)
{
Debug.Log("Arrived at connection");
ServerClient c = new ServerClient();
c.connectionId = cnnId;
c.playerName = "TEMP";
clients.Add(c);
string msg = "ASKNAME|" + cnnId + "|";
foreach (ServerClient sc in clients)
msg += sc.playerName + '%' + sc.connectionId + '|';
msg = msg.Trim('|');
Send(msg, reliableChannel, cnnId);
Debug.Log("Finished at connection");
}
private void OnNameIS(int cnnId, string playerName)
{
// link the name to the connection ID
clients.Find(x => x.connectionId == cnnId).playerName = playerName;
// Tell evertone that a new player has connected
Send("CNN|" + playerName + '|' + cnnId, reliableChannel, clients);
}
private void Send(string message, int channelId, int cnnId)
{
List<ServerClient> c = new List<ServerClient>();
c.Add(clients.Find(x => x.connectionId == cnnId));
Send(message, channelId, c);
}
private void Send(string message, int channelId, List<ServerClient> c)
{
Debug.Log("sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes(message);
foreach(ServerClient sc in c)
{
NetworkTransport.Send(socketId, sc.connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
}
private void OnDiconnection(int cnnId)
{
clients.Remove(clients.Find(x => x.connectionId == cnnId));
string msg = "DC|" + cnnId;
Send(msg, reliableChannel, clients);
}
private void OnMyPosition(int cnnId, float x, float y)
{
clients.Find(clientInQuestion => clientInQuestion.connectionId == cnnId).position = new Vector3(x, y, 0);
}
And then the client:
private const int MAX_CONNECTION = 100;
private int port = 3001;
private int hostId;
private int webHostId;
private int reliableChannel;
private int unReliableChannel;
private int connectionId;
private int clientId;
private bool isConnected = false;
public bool isStarted = false;
private float connectionTime;
private string playerName;
private byte error;
public GameObject playerPrefab;
public Dictionary<int, player> players = new Dictionary<int, player>();
public void Connect()
{
Debug.Log("connecting...");
// does player have a name?
string pName = GameObject.Find("NameInput").GetComponent<InputField>().text;
if (pName == "")
{
Debug.Log("Enter a name");
return;
}
playerName = pName;
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unReliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_CONNECTION);
hostId = NetworkTransport.AddHost(topo, 0);
connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);
connectionTime = Time.time;
isConnected = true;
Debug.Log("connected");
}
private void Update()
{
connectionTime += Time.deltaTime;
if (!isConnected)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("receiving : " + msg);
string[] splitData = msg.Split('|');
switch (splitData[0])
{
case "ASKNAME":
OnAskName(splitData);
break;
case "CNN":
SpawnPlayer(splitData[1], int.Parse(splitData[2]));
break;
case "DC":
playerDisconnected(int.Parse(splitData[1]));
break;
case "ASKPOSITION":
OnAskPosition(splitData);
break;
default:
Debug.Log("invalid message : " + msg);
break;
}
break;
}
}
private void OnAskName(string[] data)
{
// set the client ID
clientId = int.Parse(data[1]);
// send our name to the server
Send("NAMEIS|" + playerName, reliableChannel);
// create all the other players
for (int i = 2; i < data.Length - 1; i++)
{
string[] d = data[i].Split('%');
SpawnPlayer(d[0], int.Parse(d[1]));
}
}
private void OnAskPosition(string[] data)
{
if (!isStarted)
return;
//update everyone else
for (int i = 1; i <= data.Length-1; i++)
{
string[] d = data[i].Split('%');
//prevent the server from updating us
if (clientId != int.Parse(d[0]))
{
Debug.Log("updating position");
Vector3 position = Vector3.zero;
position.x = float.Parse(d[1]);
position.y = float.Parse(d[2]);
players[int.Parse(d[0])].avatar.transform.position = position;
}
}
//send out own position
Vector3 myPosition = players[clientId].avatar.transform.position;
string m = "MYPOSITION|" + myPosition.x.ToString() + '|' + myPosition.y.ToString();
Send(m, unReliableChannel);
}
private void SpawnPlayer(string playerName, int cnnId)
{
GameObject go = Instantiate(playerPrefab) as GameObject;
// is this ours?
if(cnnId == clientId)
{
// add mobility
go.AddComponent<PlayerController>();
// remove Canvas
GameObject.Find("Canvas").SetActive(false);
isStarted = true;
}
player p = new player();
p.avatar = go;
p.playerName = playerName;
p.avatar.GetComponentInChildren<TextMesh>().text = playerName;
p.connectionId = cnnId;
players.Add(cnnId, p);
}
private void Send(string message, int channelId)
{
Debug.Log("sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes(message);
NetworkTransport.Send(hostId, connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
private void playerDisconnected(int cnnId)
{
Destroy(players[cnnId].avatar);
Debug.Log("player : " + players[cnnId].playerName + " has disconnected");
players.Remove(cnnId);
}
}
Any feedback would be greatly appreciated, I have tried looking through the documentation but I feel I have done everything that it says to do in that (https://docs.unity3d.com/Manual/UNetUsingTransport.html) at the bottom it talks about webgl support, but it doesn't seem to have worked.
Fixed this issue,
Answer was that I was sending to HostID and not webSocketId
so this:
private void Send(string message, int channelId, List<ServerClient> c)
{
Debug.Log("sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes(message);
foreach(ServerClient sc in c)
{
NetworkTransport.Send(hostId, sc.connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
}
was changed to this:
private void Send(string message, int channelId, List<ServerClient> c)
{
Debug.Log("sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes(message);
foreach(ServerClient sc in c)
{
NetworkTransport.Send(webSocketId, sc.connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
}

Socket Low Transfer Speed - Win7 .Net4 - CentOSx64 Mono 2.10

I have created a simple client/server app and it works great (1MB/s which is max speed i set for server to send) when i run in locally or under Lan network. But when i try to run it in one of my Dedicate Server/VPS my download speed become slow.
I am using CentOS on servers and Mono for running it, Mono version is 2.10.2 and CentOs is 64bit version. Created using framework 4.
Speed test:
Local: 1MB
Lan: 1MB
Running server on CentOS: 0~10~20 KB
My connection speed is 2Mb or ~250KB. It will give me full speed some times. But very rare and i cant see why it give my full speed sometimes and sometimes no speed at all or why sometimes only 10KB and other times only 20KB. Also, i am running client part on my Win7 Desktop. Here is code for server and client part:
Server:
class Program
{
private static BackgroundWorker _ListeningWorker;
private static BackgroundWorker _QWorker;
private static System.Net.Sockets.Socket _Server;
private static List<System.Net.Sockets.Socket> ConnectedClients = new List<System.Net.Sockets.Socket>();
static void Main(string[] args)
{
Program._ListeningWorker = new BackgroundWorker();
Program._ListeningWorker.WorkerSupportsCancellation = true;
Program._ListeningWorker.DoWork += _ListeningWorker_DoWork;
Program._QWorker = new BackgroundWorker();
Program._QWorker.WorkerSupportsCancellation = true;
Program._QWorker.DoWork += _QWorker_DoWork;
Program.Start();
while (true)
{
Console.Clear();
Console.WriteLine("1.0.0.1");
Console.WriteLine(Program.ConnectedClients.Count.ToString());
System.Threading.Thread.Sleep(1000);
}
}
public static bool Start()
{
if (!Program._ListeningWorker.IsBusy)
{
Program._Server = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 8081);
Program._Server.Bind(ipLocal);
Program._Server.Listen(10);
Program._ListeningWorker.RunWorkerAsync();
Program._QWorker.RunWorkerAsync();
}
return true;
}
private static void _ListeningWorker_DoWork(object sender, DoWorkEventArgs e)
{
while (!Program._ListeningWorker.CancellationPending)
{
if (Program._Server.Poll(10, SelectMode.SelectRead))
{
lock (ConnectedClients)
{
Program.ConnectedClients.Add(Program._Server.Accept());
}
}
System.Threading.Thread.Sleep(1);
}
Program._Server.Close();
}
private static void _QWorker_DoWork(object sender, DoWorkEventArgs e)
{
byte[] array = new byte[1024];
Random random = new Random();
while (!Program._QWorker.CancellationPending)
{
if (ConnectedClients.Count > 0)
{
System.Net.Sockets.Socket[] st;
lock (ConnectedClients)
{
st = new System.Net.Sockets.Socket[Program.ConnectedClients.Count];
ConnectedClients.CopyTo(st);
}
foreach (System.Net.Sockets.Socket ser in st)
{
random.NextBytes(array);
try
{
ser.BeginSend(array, 0, array.Length, SocketFlags.None, (AsyncCallback)delegate(IAsyncResult ar)
{
try
{
ser.EndSend(ar);
}
catch (Exception)
{
iDissconnected(ser);
}
}, null);
}
catch (Exception)
{
iDissconnected(ser);
}
}
}
System.Threading.Thread.Sleep(1);
}
Program._Server.Close();
}
internal static void iDissconnected(System.Net.Sockets.Socket client)
{
lock (ConnectedClients)
for (int i = 0; i < ConnectedClients.Count; i++)
if (ConnectedClients[i].Equals(client))
{
ConnectedClients.RemoveAt(i);
i--;
}
}
}
Client:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter IP Address: ");
string Address = Console.ReadLine();
Console.WriteLine();
ushort Port = 8081;
System.Net.Sockets.Socket Client;
Client = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Client.Connect(Address, (int)Port);
Console.WriteLine("Connected");
int p = 0;
int t = Environment.TickCount;
while (true)
{
if (Client.Available > 0)
{
byte[] z = new byte[1024];
int r = Client.Receive(z); ;
p += r;
}
else
{
System.Threading.Thread.Sleep(10);
}
if (Environment.TickCount - t >= 1000)
{
t = Environment.TickCount;
Console.WriteLine(Program.FormatFileSizeAsString(p) + " Readed,");
p = 0;
}
}
}
public static string FormatFileSizeAsString(int len)
{
if (len < 750 && len > 0)
return "0.0 B ~";
string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
int i;
double dblSByte = len;
for (i = 0; (int)(len / 1024) > 0; i++, len /= 1024)
dblSByte = len / 1024.0;
return String.Format("{0:0.0} {1}", dblSByte, Suffix[i]);
}
}
Thanks all, Please tell me what you think about possible problems.
It was some sort of limitation by ISP per each connection.
Solving was funny. I used to send a HTTP Header before my request and ISP increased my speed because of that.