Search Unity

Unity Personal -- use SocketIOClient library to connect a node.js socket.io (NEWEST VERSION) server

Discussion in 'Scripting' started by yaakovyitzchakbenmoshe, Jan 23, 2019.

  1. yaakovyitzchakbenmoshe

    yaakovyitzchakbenmoshe

    Joined:
    Feb 9, 2018
    Posts:
    21
    Hi, I'm trying to simply connect to ANY kind of socket server with Unity personal edition (preferably with native WebSocket support but I'm not sure if that's possible in the WebGL player with the Unity Free version, let me know if there's ANY workaround though).

    SO currently I'm trying to simply connect to a socket.io server running on node.js, here's the code for the server (works PERFECTLY if I connect on a JavaScript client):
    var http = require("http");
    server = http.createServer((req,res) => {
    res.write("<b>Welcome</b>");
    res.end();
    });

    server.listen(8000, () => {
    console.log("doin it");
    });

    var io = require("socket.io")(server);
    io.on("connection", (sock) => {
    console.log("WOW can't believe it");
    });

    SO Now the Unity part:
    I've been looking online for the right library and I found this: https://github.com/NetEase/UnitySocketIO
    however, when I try to connect, I'm getting an error in unity:

    Error initializing handshake with http://localhost:8000/

    here is my unity code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class main : MonoBehaviour {

    // Use this for initialization
    void Start () {
    print("testing?");
    var cli = new SocketIOClient.Client("http://localhost:8000");
    cli.On("connect", (fn) =>
    {
    print("did I connect?");
    });
    cli.Error += (sender, e) => {
    print(e.Message.ToString());
    };
    cli.Message += onsend;
    cli.Connect();
    cli.Send("hi?");
    print("tried?");
    cli.Close();


    }
    void onopen(object sender, SocketIOClient.MessageEventArgs e)
    {
    print(e.Message);
    }
    void onsend(object sender, SocketIOClient.MessageEventArgs e)
    {
    print(e.Message);
    }
    // Update is called once per frame
    void Update () {

    }
    }

    on stackoverflow there was an answer to the problem by downgrading the socket.io to 0.9.x, but I don't want to do that.



    DOES anyone know how to simply connect to a socketIO server with the FREE version of unity? If any other kind of library at all is available, that would be great, I've been looking on the forums here and couldn't find anything really.
     
    Last edited: Jan 23, 2019
  2. thelghome

    thelghome

    Joined:
    Jul 23, 2016
    Posts:
    745
    https://forum.unity.com/threads/670270/


    We recently added a WebGL WebSocket streaming demo. This new feature supports iOS/Android/Mac/PC/WebGL build without problem.

    The updated was submitted today and will be available soon. If someone are interested, I recommend that you should get it before v1.08 released with a lower price.
     
  3. AyushParida

    AyushParida

    Joined:
    Aug 15, 2018
    Posts:
    2
    Make sure you have installed the socket.io package on node
    To install perform the following steps:-
    1. In terminal go to your server.js file
    2. Run "npm init" and press "Enter" till you have prompt to type "yes" then type "yes" and press "Enter"
    3. Run "npm install socket.io"
    4. Run "npm start"
     
  4. yaakovyitzchakbenmoshe

    yaakovyitzchakbenmoshe

    Joined:
    Feb 9, 2018
    Posts:
    21
    The question stated that the node js server is not that problem as it "works perfectly if connected via JavaScript clients"