- Home /
How to set up a UDP connection with a nodejs server in Unity?
Trying to send a buffer from a Unity C# project to a Node.js server and I can't seem to get any data to transfer. I know this could work I think I may be missing something. See code below any help is appreciated.
Node.js "Server"
var udpPort = 9082;
var url = require ('url');
var udp = require ('dgram');
var http = require ('http');
var request = require ('request');
var udpServer = udp.createSocket ('udp4').bind (9082, '127.0.0.1');
udpServer.on ('close', function (err) {
console.log ("UDP close");
});
udpServer.on ('error', function (err) {
console.log ("UDP Error: " + err.toString ());
});
udpServer.on ('message', function (data, port) {
console.log ("UDP received");
});
udpServer.on ('listening', function () {
var address = udpServer.address ();
console.log ("UDP Listening On IP: " + address.address + " at Port: " + address.port);
});
module.exports = function (){
this.send = function (objectID, script, action, variables){
}
}
Unity C# App
using UnityEngine;
using System.Text;
using System.Collections;
using System.Net.Sockets;
using System.Net;
public class Web : MonoBehaviour {
[SerializeField]
int UdpPort = 9082;
[SerializeField]
string host = "127.0.0.1";
[SerializeField]
UTF8Encoding encoding = new UTF8Encoding ();
[SerializeField]
WaitForSeconds UdpDelay = new WaitForSeconds (.3f);
[SerializeField]
IPEndPoint UdpEndPoint = null;
[SerializeField]
UdpClient client = null;
[System.Serializable]
public class JsonObject : System.Object {
public string[] values;
public JsonObject (string[] array) {
values = array;
}
}
public void UdpConnect () {
client = new UdpClient ();
}
public void UdpDisconnect () {
client.Close ();
}
void UdpReceive(byte[] data){
string JsonString = encoding.GetString (data);
JsonObject Json = JsonUtility.FromJson <JsonObject> (JsonString);
}
IEnumerator UdpCoro() {
byte[] data = null;
while (true){
if (client.Available > 0) {
yield return null;
data = client.Receive (ref UdpEndPoint);
UdpReceive (data);
data = null;
}
yield return UdpDelay;
}
}
public void UdpSend (params string[] values){
JsonObject Json = new JsonObject (values);
string Jsonstring = JsonUtility.ToJson (Json);
byte[] data = encoding.GetBytes (Jsonstring);
client.Send (data, data.Length, host, UdpPort);
}
void Start (){
UdpConnect ();
UdpSend ("Test");
}
Have not gotten to the receive part yet so thats probably wrong and can be ignored.
Thanks, Sean
Answer by STBarnard · May 25, 2016 at 12:58 AM
Set up the server as an IPEndPoint and had the client connect to it explicitly. Used client.send without specifying an arbitrary host destination. Works now.
hi i want to use udp ins$$anonymous$$d of tcp for multiplayer games with node js , could you explain more from the scratch that what packages should we install and how to coding udp ins$$anonymous$$d of tcp? if you know any tutorials i would be appreciated if you share it for me.
Hey there, I was wondering if you ever ended up figuring out how to do this from scratch or if there were any resources that you found helpful along the way. I'm currently using TCP for my multiplayer game but the latency is causing a lot of problems. Im hoping that with UDP it will run a tad smoother. Any help would be greatly appreciated!