Reputation: 73
Ok I would quickly to setup server with NodeJS to prototyping services for actual server development purposes. I would like connect to my server from game app (Unity3D based) and retrieve some from it. Currently I did tried out using Node, express and socket.io but havent get any noticed to server side but game app (Using Tcpclient class) did seems to be make some connection based on .Conneted boolean variable.
var express = require('express');
var http = require ('http');
var app = express(),
server = http.createServer (app),
io = require('socket.io').listen(server);
var jade = require('jade');
app.configure(function() {
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set("view options", { layout: false });
});
io.sockets.on('connect', function (socket) {
console.log('connected to socket.io server');
});
io.sockets.on('connection', function (socket) {
console.log ('connection established');
socket.on('setPseudo', function (data) {
socket.set('pseudo', data);
});
socket.on('message', function (message) {
socket.get('pseudo', function (error, name) {
var data = { 'message' : message, pseudo : name };
socket.broadcast.emit('message', data);
console.log("user " + name + " send this : " + message);
})
});
});
and
var port = 4100;
if (gameServer) {
gameServer.Connect(Host, port);
}
if (gameServer.Connected ==true) {
Debug.Log ("gameServer connection established");
} else {
Debug.Log ("gameServer connection failed");
}
What is need in server side to get notification when client has been connected? Or it's even possible to make websocket connection from webplayer?
Upvotes: 0
Views: 8514
Reputation: 2995
Relating to WebGL
While this is an old answer, i figured its worth having a fresh answer as things have changed recently (NPAPI support gone/going)...
WebGL is the replacement for the Web Player and going by the original, guessing they mean Web Player. So a simple answer, write a quick websocket wrapper in javascript if you plan on using WebGL
Unity / Browser Comms: http://docs.unity3d.com/Manual/webgl-networking.html
Which refers to: http://docs.unity3d.com/ScriptReference/Application.ExternalCall.html
Webpage to Unity: http://docs.unity3d.com/Manual/UnityWebPlayerandbrowsercommunication.html
I wont go into extensive, just a quick example of using JS & Unity as this should be fairly straight forward from the above links.
Also a free asset already made for >5 https://www.assetstore.unity3d.com/en/#!/content/38367e
Unity To Javascript/WebSocket
// EXECUTED IN WEB PAGE / HTML TEMPLATE
// Usual create a websocket (example only)
var socket = new WebSockect('ws://localhost:8080');
// Simple wrapper function, you should build your own class
function UnityMsgToServer(sMsg) {
socket.send(sMsg);
}
// (make sure you close the socket at the right time (if not kept alive)
socket.close();
Javascript / Websocket To Unity
// EXECUTED IN WEB PAGE / HTML TEMPLATE
var u = new UnityObject2();
u.initPlugin(jQuery("#unityPlayer")[0], "Example.unity3d");
function SaySomethingToUnity() {
u.getUnity().SendMessage("MyObject", "MyFunction", "Hello from a web page!");
}
Appending More Js To Webpage (various uses ;)
// EXECUTED IN UNITY PROJECT
Application.ExternalEval(
"var oSocket = new WebSockect('ws://localhost:8080');"
);
Upvotes: 0
Reputation: 626
I think you cannot use the basic unity tcp class. I never tried to make a multiplayer game in Unity3D, but I would suggest you to look at this https://github.com/NetEase/UnitySocketIO as it might be the thing you are looking for.
Upvotes: 1