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!

2
  • Did you ever figure out the problem?
    – Harrison
    May 28, 2015 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, 2015 at 15:00

3 Answers 3

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.

3
  • What version/release are you using for UnitySocket-IO? Could you link me to it please? Jul 24, 2014 at 13:10
  • Newest, Link, I just pressed download as zip.
    – Wazzaps
    Jul 25, 2014 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"); Jul 29, 2014 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 ...

1
  • Welcome to stack overflow. Please consider using the space provided to elaborate on the external content from your link.
    – sao
    Feb 12, 2020 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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.