Search Unity

Unity & NodeJS backend server.

Discussion in 'Multiplayer' started by redigaffi, May 29, 2015.

  1. redigaffi

    redigaffi

    Joined:
    May 29, 2015
    Posts:
    2
    Just got it. I'm new to Unity (and to this community, hello :p), but im developer since 5 years. Yesterday i did some research, if it were possible to have a unity client, and a nodeJS server, i haven't found any guides or semething. After many hours of researching, reading, trying, viewing videos, and ma' own, i've got it.

    Little chat server written in nodeJS. Now i gonna develop a script to draw all players... I think i gonna share my work, but it's really easy, all goes through 'websockets', all you need is websocket for sharp, and for nodeJS.

    - https://github.com/sta/websocket-sharp
    - https://github.com/websockets/ws

    Maybe nodejs is not the most efficient way, but many people like it and it's very easy and can load lot's of traffic.



    PD: im new to forum :p
     
  2. RobinKuiper

    RobinKuiper

    Joined:
    Jul 23, 2015
    Posts:
    3
    Hi,

    Sounds interesting!

    How did you connect them tho?

    At the moment I have this set up as a simple Node Websocket server:
    Code (CSharp):
    1. var WebSocketServer = require('ws').Server
    2.     , wss = new WebSocketServer({ port: 9092 });
    3.    
    4. console.log('Server running on port 9092.');
    5.    
    6. wss.on('connection', function connection(ws){
    7.     console.log('Client connected');
    8.     ws.on('message', function incoming(message){
    9.         console.log('Received: %s', message);
    10.     });
    11.    
    12.     ws.send('blaat');
    13. });
    And this in my Unity project:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using WebSocketSharp;
    4.  
    5. public class Node : MonoBehaviour {
    6.  
    7.     void Start () {
    8.         using (var ws = new WebSocket ("ws://127.0.0.1:9092")) {
    9.             ws.OnMessage += (sender, e) =>
    10.                 Debug.Log (e.Data);
    11.            
    12.             ws.Connect ();
    13.             ws.Send ("message");
    14.         }
    15.     }
    16.  
    17. }
    18.  
    According to the examples on the repositories you linked. But it doesn't do anything.

    Thanks!
     
  3. Christian-Tucker

    Christian-Tucker

    Joined:
    Aug 18, 2013
    Posts:
    376
    I would highly recommend using a proper TCP implementation instead of websockets(Not saying websockets aren't built on the TCP protocol). However you can achieve everything you'd want using a TcpClient and it's much easier to manage. Considering javascript doesn't understand binary formats. I'd recommend something like ``Socket.io`` to make your experience really easy.
     
  4. RobinKuiper

    RobinKuiper

    Joined:
    Jul 23, 2015
    Posts:
    3
    Thanks!

    Is there a way to use socket.io in Unity with C#?
     
  5. Christian-Tucker

    Christian-Tucker

    Joined:
    Aug 18, 2013
    Posts:
    376
    Absolutely, however you'll have to handle the special events yourself. Socket.io is just built ontop of the TCP Layer, but NodeJS has it's own TCP server that you can run. (Which I would personally use, as it's no extra hassle for a little simplicity.)

    To create a TCP server in NodeJS you need to make use of "net"

    Code (csharp):
    1.  
    2. net = require('net');
    3. net.createServer(function (socket) {
    4.  
    5. }).listen(5055);
    6. console.log("Server is listening on port 5055");
    7.  
    See: https://gist.github.com/creationix/707146

    -- The C# TcpClient can connect to this by default.

    NOTE:
    Keep in mind that NodeJS (JavaScript) does not understand Binary values, so you'll have to transmit your data in a JSONText format.
     
  6. RobinKuiper

    RobinKuiper

    Joined:
    Jul 23, 2015
    Posts:
    3
    Thank you very much! Will try it out!
     
  7. 8Observer8

    8Observer8

    Joined:
    Apr 29, 2015
    Posts:
    99
  8. sdocktor

    sdocktor

    Joined:
    Jul 4, 2017
    Posts:
    2
    I've used UnityWebRequest to get the data from my NodeJS-Server. Works fine for me.