Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 STBarnard · Mar 15, 2016 at 11:46 AM · c#udpnodejs

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

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · 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 ElegantUniverse · Apr 28, 2020 at 06:08 PM 0
Share

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.

avatar image SaberAlex ElegantUniverse · Jun 16, 2020 at 03:48 PM 0
Share

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!

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.

Follow this Question

Answers Answers and Comments

109 People are following this question.

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

Related Questions

Unity to Node.js UDP: datagrams received are all empty. 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

What's the best approach for creating a secure authorative server? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges