Quantcast
Channel: Questions in topic: "nodejs"
Viewing all articles
Browse latest Browse all 130

How to set up a UDP connection with a nodejs server in Unity?

$
0
0
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 (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

Viewing all articles
Browse latest Browse all 130

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>