2

I am searching for a way to create a unity3d game with a node.js server. I am familiar with Socket.io so I wanted to use that.

I got "UnitySocket-IO" but it doesn't work, It says: Error initializing handshake with http://localhost:80/

My code on the client (Unity) is:

#pragma strict
var client:SocketIOClient.Client = new SocketIOClient.Client("http://localhost:80/");

function Start () {
    //client.Opened += SocketOpened;
    client.Message += SocketMessage;
    //client.SocketConnectionClosed += SocketConnectionClosed;
    client.Error +=SocketError;
    client.Connect();
}

function SocketMessage(sender, e) {
    client.Send("Pong");
}

function SocketError(sender, e:SocketIOClient.ErrorEventArgs) {
    Debug.Log(e.Message);
}

The code on the server (Node.JS):

var io = require('socket.io')();
io.listen(80);

io.on("connection", function (socket) {
    "use strict";
    console.log("Connected!");
    setTimeout(function () {
        console.log("Sent ping.");
        socket.emit("Ping!");
    }, 2000);
    io.on("message", function (data) {
        console.log(data);
    });
});

I suspect this is a problem between 1.0 and the unitysocket-io library. Is there any alternative that works in Unityscript?

I tried downgrading to 0.9, but it still doesn't work, and I want to use current versions of things. Thanks!

Wazzaps
  • 380
  • 5
  • 11
  • Did you ever figure out the problem? – Harrison May 28 '15 at 11:52
  • @Harrison Nope, created a local node server which communicated with the Unity instance using UDP as a form of IPC, while also (the node server) hosting a socket.io server to communicate via the internet. Hackity hack. – Wazzaps Aug 29 '15 at 15:00

3 Answers3

0

You might want to try with other ports. Port 80 is usually used by other applications or processes, and if it is in use, socket.io won't be able to use it. Change it to 3000 or something you know is unused to see if that solves the problem.

Blubberguy22
  • 1,344
  • 1
  • 17
  • 29
  • What version/release are you using for UnitySocket-IO? Could you link me to it please? – Blubberguy22 Jul 24 '14 at 13:10
  • Newest, [Link](https://github.com/NetEase/UnitySocketIO/tree/master/SocketIO/bin/Debug), I just pressed download as zip. – Wazzaps Jul 25 '14 at 10:51
  • Try changing the creation of your client to `var client:SocketIOClient.Client = new SocketIOClient.Client("http://localhost:80/socket.io/?EIO=2&transport=polling");` – Blubberguy22 Jul 29 '14 at 12:45
0

to use Socket IO in unity you can use simple package in this link : socket.io in unity

and drag and drop SocketIo prefab in you hierarchy. in this prefab is an script have url change url and port for your localhost. *change port 80 by another port because port 80 is the port number assigned to commonly used internet communication protocol, Hypertext Transfer Protocol (HTTP). e.g : port 3000 3001 ...

  • Welcome to stack overflow. Please consider using the space provided to elaborate on the external content from your link. – sao Feb 12 '20 at 18:06
0

https://github.com/Quobject/SocketIoClientDotNet Although this project has deprecated, but it is still usable.

Tested on Unity 2019. Do note that SocketIO has to be version 2.1.1

joantoh
  • 81
  • 1
  • 11