hi dears, "please sorry for my bad english". I am improving a UDP connection to muy plugin-web development, in first time i start my own code, but after a few days when i cant does work correctly i refer to this guy:
How to set up a UDP connection with a nodejs server in Unity? - Unity Answers
And start to migrate my code in his development code, obviosly adapting the comunication between my plugin and his monoBehavior class because he is a direct unity developer. But okey.
I can send DATA of my plugin to the NODE.JS but seems to be impossible to send data from my NODE.JS server to my plugin i have more than 1 week in this and i start to be desesperated.
my plugin code is:
i send info to the NODE.JS viaCode:using System; using System.IO; using System.Timers; using System.Reflection; using System.Collections.Generic; using System.Collections; using System.Data; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Net.Sockets; using System.Net; //using System.Net.IPAddress; using System.Threading;using Oxide.Core; using Oxide.Core.Libraries; using Oxide.Core.Plugins; using Oxide.Core.Configuration; using Oxide.Core.Logging; using Oxide.Core.Libraries.Covalence;using Newtonsoft.Json; using Newtonsoft.Json.Converters;using UnityEngine; using Facepunch; using Rust; using Random = System.Random;namespace Oxide.Plugins { [Info("ModongaEvent", "MODONGA colaboration STBarnard code", "1.0.4", ResourceId = 652), RequireComponent(typeof(i_love_codeoSource))] class ModongaEvent : RustPlugin { #region CLIENT_TO_SERVER CLIENT_TO_SERVER Servidor; GameObject WebObject; public class CLIENT_TO_SERVER : MonoBehaviour { #region DECLARACIONES DEL SERVIDOR int UdpPort = 3000; string host = "192.168.1.2"; IPEndPoint server; UdpClient client = new UdpClient(); UTF8Encoding encoding = new UTF8Encoding(); Thread networkThread; IPEndPoint remoteEndPoint; #endregion // HERE IS THE PROBLEM------------------------------------- // HERE IS THE PROBLEM------------------------------------- void UDPThread () { while (true) { byte[] buffer = client.Receive (ref server); UDPReceive (buffer); } } void UDPReceive(byte[] buffer){ string message = encoding.GetString (buffer); JsonObject json = JsonUtility.FromJson <JsonObject> (message); Debug.Log (json); //see message in console } // HERE IS THE PROBLEM------------------------------------- // HERE IS THE PROBLEM------------------------------------- public void SendData(string action, string JSON) { if (action == "start") {UDPConnect();} if (action == "send"){UdpSend(JSON);} } public void UDPConnect() { //System.Net.IPAddress ipaddress = System.Net.IPAddress.Parse("127.0.0.1"); //127.0.0.1 as an example //IPAddress ipaddress = IPAddress.Parse("127.0.0.1"); //remoteEndPoint = new IPEndPoint(IPAddress.Parse(host), UdpPort); networkThread = new Thread(new ThreadStart(UDPThread)); networkThread.IsBackground = true; client = new UdpClient(); networkThread.Start(); } public void UDPDisconnect() { networkThread.Abort(); client.Close(); } [System.Serializable] public class JsonObject : System.Object { public string target; public string action; public string [] values; public JsonObject (string target, string action, string[] array) { this.target = target; this.action = action; values = array; } } public void UdpSend(string values) { byte []bytes = System.Text.Encoding.UTF8.GetBytes(values); client.Send(bytes, bytes.Length, host, UdpPort); } } #endregion #region DATA CONSTRUCT TO SEND public class Chat { public string seccion; public string jugador; public string mensaje; } //send data message to MonoBehaviour void OnUserChat(IPlayer player, string message) { Chat chat_obj = new Chat(); chat_obj.seccion = "chat"; chat_obj.jugador = player.Name.ToString(); chat_obj.mensaje = message; string json = JsonUtility.ToJson(chat_obj); Servidor.SendData("send", json);//<- send message } #endregion #region INITIALIZE SERVER & UNLOAD SERVER void OnServerInitialized(){} void Unload() { UnityEngine.Object.Destroy(WebObject); Servidor = null; } void OnPluginLoaded() { WebObject = new GameObject("WebObject"); Servidor = WebObject.AddComponent<CLIENT_TO_SERVER>(); //timer.Repeat(0.1f, 0, () => SEND_DATA_FUNCTION()); Servidor.SendData("start", "");//<- send start the UDP client } #endregion } }
and i receive from the NODE.JSCode:Servidor.SendData("start", "");//<- send start the UDP client
but i never receive anithyng i try to useCode:void UDPThread () { while (true) { byte[] buffer = client.Receive (ref server); UDPReceive (buffer); } }
but the console says: System acces is restricted, you are not allowed to use System.Net.IPAddresCode:System.Net.IPAddress ipaddress = System.Net.IPAddress.Parse(ip); IPAddress ipaddress = IPAddress.Parse(ip); remoteEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
i think and appear that the compiller of oxide dont have a premium unity licence to use System.Net.IPAddres
Creating a UDP client for Node.js?
Discussion in 'Rust Development' started by modonga, Jun 4, 2016.
-
Below my NODE.JS server
Code://--------------------------------------CREAR SERVIDOR W&U--------------------------------------// //--------------------------------------CREAR SERVIDOR W&U--------------------------------------// //--------------------------------------CREAR SERVIDOR W&U--------------------------------------// //--------------------------------------CREAR SERVIDOR W&U--------------------------------------// //--------------------------------------CREAR SERVIDOR W&U--------------------------------------// var express = require('express'); filesystem = require('fs'), app=express(); server = require('http').createServer(app); io = require('socket.io').listen(server); server.listen(3000); var udpServer = require ('dgram').createSocket ('udp4').bind (3000, '192.168.1.2'); //--------------------------------------SERVIDOR WEB--------------------------------------// //--------------------------------------SERVIDOR WEB--------------------------------------// //--------------------------------------SERVIDOR WEB--------------------------------------// //--------------------------------------SERVIDOR WEB--------------------------------------// //--------------------------------------SERVIDOR WEB--------------------------------------// app.use('/public', express.static(__dirname+'/public')); app.get("/",function(req,res){ res.sendFile(__dirname + '/public/clientes.html'); }); io.sockets.on('connection',function(socket){ //recuperar historial para nuevo usuario filesystem.readFile('historialChat.dll', 'utf8', function (err,data) { if (err){return console.log ("Error al abrir historial de CHAT");} io.sockets.emit('newMessage',{mensaje: data}); }); socket.on('sendMessage',function (data){ //io.sockets.emit('newMessage',{msg: data}); //udpServer.emit('newMessage',{msg: data}); }); }); //--------------------------------------SERVIDOR UNITY--------------------------------------// //--------------------------------------SERVIDOR UNITY--------------------------------------// //--------------------------------------SERVIDOR UNITY--------------------------------------// //--------------------------------------SERVIDOR UNITY--------------------------------------// //--------------------------------------SERVIDOR UNITY--------------------------------------// udpServer.on ('message', function (value, rinfo) { data = JSON.parse(value); console.log ("RUST: " +data.seccion); var construct=("<p class='chat_name'>"+data.jugador+":</p><p class='chat_message'>"+data.mensaje+"</p></br>"); filesystem.appendFile('historialChat.dll', construct, function (err) { if (err){return console.log ("Error al guardar historial de CHAT");} }); if (data.seccion=="chat"){ io.sockets.emit('newMessage',{mensaje: construct}); } var buf = new Buffer("my data!", "utf8"); send (buf, rinfo.port, rinfo.address);//<- this is the ping pong method then i send something via plugin appear to send but any data is received in plugin :(});function send (message, port, address) { udpServer.send (message.toString (), message.length, 0, port, address, function (err) { if (!err) { console.log ('UDP => Sending: ' + message); } else { console.log (err); } }); }udpServer.on ('listening', function () { var address = udpServer.address (); console.log ("UDP Listening On IP: " + address.address + " at Port: " + address.port); });
i receive perfectly from my plugin via this event:
Code:udpServer.on ('message', function (value, rinfo) { bla bla bla
Code:send (buf, rinfo.port, rinfo.address);
but i dont receive anything in the plugin.
Any help is really appreciated
Modonga. -
I'm pretty sure you're not allowed to open any UDP sockets on your server via Oxide (for security reasons, as plugins are sandboxed).
You can send GET and POST-requests via the WebRequests library (Oxide/WebRequests.cs at master · OxideMod/Oxide · GitHub), which allows you to unidirectionally communicate with your NodeJS server from your plugin, but not the other way around (your NodeJS server can only reply to the request, not initiate a connection by itself).
If that is sufficient, then go with that.
Another solution I can think of is to use the MySQL extension to share state using a database between your NodeJS server and your plugin. -
i think GET & POST solution.... mmm could be so slow for that i am searching, with files definitively no but with MySQL maybe. Thanks for the reply!
in another hand, why i can recive perfectly the info from the plugin? so the problem or the solution... is to find the way to send info to my plugin any idea?
extra info: my server nodeJS and my rust server are in the same machine (PC) dont know other ways to send and receive info to my NODE.JS server
Sorry i was literally dont read that part...this is so sad
so how can i "reply to the request" whit my NodeJS server ? that is suficcient for me -
Wulf Community Admin
-
-
Wulf Community Admin
-
thanks for the info! Wulf