I'm testing with socket.io and unity.
In my localhost (my pc) did walk perfectly but now I got all on my hosting to see how it works online and i have the following problem:
url app online: http://almightysystem.com.ar/UnityApps/UnityChatSocket.io/
I have this loop error:
GET http://almightysystem.com.ar:29011/socket.io/?EIO=3&transport=polling&t=1423454072758-3 net::ERR_CONNECTION_TIMED_OUT
server.js:
var express = require('express');
var port = process.env.PORT || 29011;
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(__dirname + '/public'));
io.on('connection', function(socket){
socket.on('chat message', function(message){
io.emit('chat message', message);
});
});
http.listen(port, function(){
console.log('Express server listening on port ' + port);
});
client.js:
//SOCKET.IO
var socket = io.connect('http://almightysystem.com.ar:29011/');
socket.on('chat message', function(message){
SendMessage ('ChatSystem', 'GetChatMessage', message);
console.log("Mensaje recibido del Servidor hacia Unity: " + message);
});
function UnitySendMessage(message){
socket.emit('chat message', message);
console.log("Mensaje de Unity al Servidor: " + message);
}
Any idea?
I have an answer to this problem.
In your client code.
// SOCKET.IO
var socket = io.connect('http://almightysystem.com.ar:29011/');
socket.on('chat message', function(message){
SendMessage ('ChatSystem', 'GetChatMessage', message);
console.log('Mensaje recibido del Servidor hacia Unity: ' + message);
});
function UnitySendMessage(message){
socket.emit('chat message', message);
console.log('Mensaje de Unity al Servidor: ' + message);
}
Remove the port number in the connect function.
var socket = io.connect('http://almightysystem.com.ar:29011/');
Like this.
var socket = io.connect('http://almightysystem.com.ar');
In your server code, you should use server.listen(port, ...) and not app.listen(port, ...) because app.listen() creates a new http server and attaches to that, which you do not want because you already have one (server).
Also, in your client code, you should use io.connect('http://almightysystem.com.ar:29011/'); instead of io.connect('http://almightysystem.com.ar/UnityApps:29011'); because the socket.io server won't know to listen on /UnityApps (because you're not passing a config object (with path set) to the socket.io server constructor), even after you've moved the :29011 part to the right place.
I had the same problem and solved it using
var socket = io.connect();
instead of
var socket = io.connect('http://almightysystem.com.ar:29011/');
Try this on your client side const socket = io("http://almightysystem.com.ar:29011/", { transports: ["websocket"] }); to specify you want to use websockets
Related
When users do a GET /check/health, this client should talk to Server and sever should give the client the answer..
But the message from the server is not received on the client..
Client side - also acting as a webserver
var io = require('socket.io-client');
var socket = io.connect('http://localhost:4000', {reconnect: true});
var express = require('express');
var app= express();
var path = require('path');
var bodyParser= require('body-parser');
app.use(express.static(__dirname+"/public/"));
app.use(bodyParser.json());
app.set('views',path.join(__dirname,'/public/html'));
app.engine('html', require('ejs').renderFile); //specify which template engine to use
app.set('view engine', 'ejs');
app.get('/check/health',function(req,res){
//console.log('Connected Success!!');
socket.on('connect', function(socket) {
console.log('Connected!');
});
socket.emit('data', 'I need your health status');
socket.on('data', function(data) {
console.log('Message from monitoring is : ' + ': ' + data);
});
socket.on('server data', function(data) {
console.log('Received server data: ' + data);
});
});
app.listen(3000);
console.log("Server running at http://localhost:3000/'");
Server side:
var app = require('express')();
var SERVER = require('http').Server(app);
var io = require('socket.io')(SERVER);
var express = require('express');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/sensor_db');
io.on('connection', function(socket){
console.log('connection received from Provisioning ');
// To get messages from Provisioning server
socket.on('data', function(data) {
console.log('Message from provision is : ' + ': ' + data);
});
socket.emit('server data', 'Here is yiour data - 1111');
});
SERVER.listen(4000, function(){
console.log('listening on *:4000');
});
There are a number of potential issues here, but the main one is that your server side code is missing the following very important line:
http.listen(4000);
Adding that should get you started down the right path. Also, I would suggest renaming the http variable to something else, since it's not the http module. server makes more sense to me.
Here's a more minimal example of what you're looking to do. It's missing a few things such as error handling, considering what should happen when a request to /check/health comes in and your socket.io connection isn't up, etc, but I'll leave that as an exercise for you. I also trimmed out some stuff that wasn't relevant to the question (mongoose, ejs templating, etc), so you'll have to add those back in when you're confident that this piece is working as intended.
Client Side
var io = require('socket.io-client');
var socket = io.connect('http://localhost:4000', { reconnect: true });
var express = require('express');
var app= express();
var path = require('path');
// careful here -- the socket.io connection will be made
// outside of the context of the /check/health callback,
// so you should move the connect event handler out here.
socket.on('connect', function(socket) {
console.log('Connected!');
});
app.get('/check/health',function(req,res){
// note the third argument here,
// which can be used as an acknowledgement from the server
// that your client's emit was received
socket.emit('data', 'I need your health status', function ack(data) {
console.log('data emit was acknowledged:', data);
// make sure you send something back to the requester
// or they'll just hang until timeout
return res.json(data);
});
// if you want, you could technically use socket.once('server data'),
// in this location, but this is probably going to be closer
// to the style of communication you actually want --
// which is one response to this specific single socket emit.
});
app.listen(3000);
console.log('Server listening at port 3000');
Server Side
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var express = require('express');
io.on('connection', function(socket){
console.log('connection received from Provisioning');
// To get messages from Provisioning server
socket.on('data', function(data, ack) {
console.log('Message from provision is : ' + ': ' + data);
ack('here is your data - 1111');
});
});
server.listen(4000, function(){
console.log('socket.io server listening on *:4000');
});
I am trying to emit message from client side with socket.io ...
Here is my client code:
var socket = io.connect('http://localhost/');
socket.on('connect', function(data){
setStatus('connected');
socket.emit('subscribe', {channel:'update.comment'});
});
Server:
io.sockets.on('connection', function (socket) {
socket.emit('message', { text : 'Welcome!' });
socket.on('subscribe', function (data) {
socket.join(data.channel);
redisClient.subscribe(data.channel);
});
});
Also I get this error message in console:
GET
http://localhost/socket.io?EIO=3&transport=polling&t=1442169984269-1
404 (Not Found)
Full serever:
var app = require('express')();
var http = require('http').Server(app);
var redis = require('ioredis');
var io = require('socket.io')(http);
redisClient = redis.createClient();
//look for connection errors and log
redisClient.on("error", function (err) {
console.log("error event - " + redisClient.host + ":" + redisClient.port + " - " + err);
});
io.sockets.on('connection', function (socket) {
socket.emit('message', { text : 'Welcome!' });
//on subscription request joins specified room
//later messages are broadcasted on the rooms
socket.on('subscribe', function (data) {
socket.join(data.channel);
redisClient.subscribe(data.channel);
});
});
redisClient.on('ready', function(data) {
console.log('#redis ready');
});
redisClient.on("message", function(channel, message){
console.log(channel);
var resp = {'text': message, 'channel':channel};
io.sockets.in(channel).emit('message', resp);
});
http.listen(3000, function(){
console.log('Listening on Port 3000');
});
New Problem Recognized:
Your server is listening on port 3000, but you are attempting to connect on port 80. The error message http://localhost/socket.io?EIO=3&transport=polling&t=1442169984269-1 has no port number on it so that defaults to port 80.
That error message means that your server-side socket.io code is not initialized correctly and thus is not listening for the HTTP request that starts all webSocket connections so when the browser tries to connect on that URL to initiate a socket.io connection, there's nobody on the server-side listening so the web server returns a 404 error back to the browser.
If you are using Express, this is the minimal socket.io initialization to hook it into your server:
var express = require('express');
var app = express();
var server = app.listen(8081);
var io = require('socket.io').listen(server);
For a plain HTTP server, this is the minimal socket.io initialization:
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
app.listen(80);
As always, if you show us the socket.io and web server initialization code you are using, we can help you better with your specific code issue.
I'm trying to make a simple IRC to get to understand how things work. I've set up a local node server and another workspace to connect to localhost, they talk just fine. However when I push the local server to Openshift and try to connect to it with the client with;
var socket = io.connect("http://irc-abc.rhcloud.com:3000");
It's not connecting.
What am I doing wrong here and or how can I make this work?
NOTE: I'm new to websockets and I'm trying to understand them, I'm probably missing the big picture.
Yes I did find a lot of similar questions on stackoverflow however it did not help me, as I said I'm fairly new with websockets and have some trouble understanding it.
server
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var ip = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var port = process.env.OPENSHIFT_NODEJS_PORT || 3000;
// TO-DO verify creds against db
var credentials = {
'username' : ''
};
io.on('connection', function(socket){
// verify if client has username
socket.on('login', function(credentials){
if(credentials.username) {
this.username = credentials.username;
console.log( this.username + ' connected');
io.emit('notice', this.username + ' connected');
}
else {
console.log( 'USER GOT DENIED' );
socket.disconnect();
}
// when message received
socket.on('chat message', function (msg) {
var message = this.username + ': ' + msg;
io.emit('chat message', message);
});
});
// when user disconnects
socket.on('disconnect', function(){
console.log( this.username + ' disconnected');
});
});
http.listen(port, ip);
console.log('listening on ' + ip + ':' + port);
Client
var socket = io.connect("irc-abc.rhcloud.com:3000");
var username = '<?php if(isset($_SESSION['username'])) echo $_SESSION['username'];
else echo 'guest'; ?>';
var credentials = {
'username' : username
};
socket.emit('login', credentials);
$('form').submit(function() {
if ($('#m').val()) {
socket.emit('chat message', $('#m').val());
$('#m').val('');
}
window.scrollTo(0,document.body.scrollHeight);
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
window.scrollTo(0,document.body.scrollHeight);
});
socket.on('notice', function(notice){
$('#messages').append($('<li>').text(notice));
window.scrollTo(0,document.body.scrollHeight);
});
The first thing I'd do is check if port 3000 is firewalled. Some (most) hosts firewall all ports that are not normally used (80, 443, etc). If you can't get them to change the firewall right away, test with port 80.
I am currently learning to use socket.io with node js but I'm having a hard time because I think something may have changed between versions. I have a litte demo using 1.0.4 in which I use something like this to send events from the client and receive them in the server:
SERVER
var socketio = require('socket.io');
var http = require('http');
var app = express();
var server = http.Server(app);
var io = socketio.listen(server);
var port = process.env.PORT || 8080;
server.listen(port, function(){
console.log("Express server listening on port " + port);
});
io.on('connection', function (socket) {
socket.emit('connected');
socket.on('myEvent', function(){
console.log('myEvent has been emitted');
});
});
CLIENT
$(document).ready(function(){
$('#button').click(function(){
emitEvent();
});
});
var socket = io.connect('http://localhost:8080/');
socket.on('connected', function () {
alert('server says I am connected');
});
function emitEvent(){
socket.emit('myEvent');
}
With both versions I can open the socket on the client and receive the 'connected' event sent later from the server than launches the alert function. The problem here is when I want to send any other event from the client. "socket.emit('myEvent');" in the emitEvent function seems to work fine for the 1.0.4 version but not for the 1.0.6 version. I have been looking for info about the changes and trying to understand the whole module but cannot get to the solution. Does anyone know what am I doing wrong? Obviously the way sending client events has changed. I would appreciate if someone could help me with this issue. Thanks in advance.
I didn't understand the problem actually. But here's the code for your functionality.
client:
var socket = io.connect();
$('#button').click(function(){
socket.emit('myEvent');
});
socket.on('connected', function(){
alert "you are connected";
});
server:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(app);
var port = process.env.PORT || 8080;
app.listen(port);
io.on('connection', function(socket){
socket.on('myEvent', function(){
socket.emit('connected');
console.log('emmited succesfully');
});
});
I have srcds (source dedicated server)
at console add logaddress_add 0.0.0.0:25001
this turn on sending the log to the remote server
tried to catch the log in this way
var net = require('net');
var server = net.createServer(function(c) {
c.on('end', function() {
console.log('server disconnected');
});
c.pipe(c);
});
server.listen(25001);
and that
var net = require('net');
var client = net.connect({port: 25001});
client.on('data', function(data) {
console.log(data.toString());
client.end();
});
client.on('end', function() {
console.log('client disconnected');
});
and that
var s = dgram.createSocket('udp4');
s.bind(25001, function(data) {
console.log(data)
});
no result. can someone help?
thanks in advance
[solved]
at SRCDS server
logaddress_add 0.0.0.0:8006 //for local ip
at app.js
var dgram = require('dgram'),
server = dgram.createSocket('udp4');
server.on('message', function (message, rinfo) {
var msg = message.toString('ascii').slice(5,-1);
console.log(msg);
});
server.on('listening', function () {
var address = server.address();
console.log('UDP Server listening ' + address.address + ':' + address.port);
});
server.bind(8006);
I ended up writing a little library to do this (srcds-log-receiver), which validates the packet format, extracts out the date and allows you to use the sv_logsecret function to put a small amount of authentication on the connection, since UDP packets are easily forged.
I also wrote a parser to turn those log lines in to useful objects.