I am beginner to socket.io and trying to make simple hello world like program. It looks like my index.html page loads and connects to the server successfully but the server is not firing the "connection" event. I am sharing my code so that you can figure out what am I doing wrong here.
app.js
var app = require('http');
var io = require('socket.io'), fs = require('fs');
app.createServer(function(req,res){
console.log(req.url);
switch(req.url){
case '/':
fs.readFile('./index.html',function(err,data){
if(err){
res.end('Error');
throw err;
}
res.writeHead(200,{"Context-type":"text/html"});
res.write(data);
res.end();
});
break;
case '/socket.io.js':
fs.readFile('./socket.io.js',function(err,data){
if(err){
res.end('Error');
throw err;
}
res.writeHead(200,{"Context-type":"text/javascript"});
res.write(data);
res.end();
});
}
}).listen(8888);
ios = io.listen(app);
console.log("Listening..");
ios.sockets.on('connection',function(socket){
console.log('connection established.'); //this doesn't show up in CLI
});
Index.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Example</title>
<script src="socket.io.js" type="text/javascript"></script>
<script>
var socket = io.connect('http://localhost:8888');
</script>
</head>
<body>
</body>
</html>
Finally I figured it out myself. All I had to do was to
server = require('http').createServer()
and then pass that server to the listener of socket io i.e.
io.listen(server);
What I was doing wrong that I was passing app = require('http'); to the io.listen(app)
Related
I'm just getting started again with Node and Socket IO. I'm using socket.io v1.3.7.
I can load the socket IO client but it is long polling instead of using sockets.
The last time I used socketIO it worked fine. I think that was the 0.9.x branch.
http://sockettest.dev:3000/socket.io/?EIO=3&transport=polling&t=1447350154198-2&sid=eW7B_kpSat6WfGQSAAAC
Here's the code..
Server - app.js
var server = require('http').createServer();
var io = require('socket.io')(server);
io.on('connection', function(socket) {
socket.emit('connection', 'connected');
socket.on('disconnect', function() {
socket.emit('disconnection', 'not connected');
});
});
server.listen(3000);
Client - index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="//recently.dev:3000/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('<?php echo '//' . $_SERVER['HTTP_HOST'] . ':3000/'; ?>');
socket.on('connection', function(data){
console.log(data);
});
</script>
</head>
<body>
</body>
</html>
I think this is why..
Why is my socket.io using long polling instead of the websocket?
I think older approach was better IMO. There should at least be an option.
I uploaded a server-side code to AWS Amazon EC2 with node.js and socket.io.
When I connect to that server from browser(both on desktop and ios safari), it works with no problem.
But, when I create a cordova project and modify some of the codes to comply with cordova, get it run on ios device, it doesn't work!
I tried
<access origin="My-server-side-url-comes-here*"/>
but it didn't solve the problem.
Also, i opened up port :9000 on EC2 for TCP connection.
It seems that socket = io.connect(serverUrl); is causing problem..
I've been struggling to get this work for days now...
Can anyone tell me how to get socket.io work with external host, phonegap, and ios?
server side code uploaded to AWS Amazon EC2 (app.js)
var http = require('http');
var socketio = require('socket.io');
var server = http.createServer(function (request, response){
console.log('server created');
}).listen(9000, function(){
console.log('server running!');
});
var io = socketio.listen(server);
io.sockets.on('connection', function(socket){
socket.on('msg', function(data){
console.log(data);
io.sockets.emit('msg_by_server', "server got your msg:"+data);
});
});
client-side code that works with desktop browsers and iPhone safari(which works fine) (index.html)
<html>
<head>
<script src="https://cdn.socket.io/socket.io-1.0.0.js"></script>
<script>
var socket;
var serverUrl = "sampleURL.compute.amazonaws.com:9000";
window.onload = function(){
socket = io.connect(serverUrl);
socket.on('msg_by_server', function(data){
alert(data);
});
}
var sendMessage = function(){
socket.emit('msg', 'msg from client');
}
</script>
</head>
<body>
<button onclick = 'sendMessage();'>Message Send</button>
</body>
</html>
different client-side code used for phonegap(which doesn't work) (index.html)
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="http://cdn.socket.io/socket.io-1.0.3.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
alert('check1');
var socket="";
app.initialize();
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
alert('check2'); // shows up
var serverUrl = "sameURL.compute.amazonaws.com:9000";
socket =io.connect(serverUrl);
alert('check3'); // doesn't show up
socket.on('connect', function(){
socket.on('msg_by_server', function(data){
alert(data);
});
socket.emit('msg', 'msg from client')
});
}
var sendMessage = function(){
alert(socket);
socket.emit('msg', 'msg from client');
};
</script>
</head>
<body onload="onDeviceReady();">
<!--
i put onload="onDeviceReady();" because
document.addEventListener("deviceready", onDeviceReady, false);
doesn't seem to work for some reason..
-->
<button onclick = 'sendMessage();'>amessageSend</button>
</body>
</html>
I’m having a problem getting started with Node.js.
I’ve created a basic server that I know works, because if I navigate to http://localhost:5000 in my browser I get the expected message. However, I’m having trouble then connecting to this server on the client side with a basic HTML page.
My Node.js app looks like this:
var http = require('http');
var socket = require('socket.io');
var port = process.env.PORT || 5000;
var players;
var app = http.createServer(function(request, response) {
response.write('Server listening to port: ' + port);
response.end();
}).listen(port);
var io = socket.listen(app);
function init() {
io.configure(function() {
io.set('transports', [ 'xhr-polling' ]);
io.set('polling duration', 10);
});
io.sockets.on('connection', onSocketConnection);
};
function onSocketConnection(client) {
console.log('New connection');
console.log(client);
};
init();
My HTML page looks like this (based on https://github.com/mongolab/tractorpush-server/blob/master/index.html):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('/');
socket.on('all', function(data) {
console.log(data);
});
socket.on('complex', function(data) {
console.log(data);
});
</script>
</body>
</html>
I understand that the sockets.io.js file is automatically generated by socket.io, but I just get the following error when I view my index.html file:
Uncaught ReferenceError: io is not defined
How do I actually connect to my server?
If I start the server and go to localhost:4000, the server gives out the index.html. But the index.html does not load its style.css and does not connect to the server. But if I double click the local index.html document it connects to the server. What is wrong about my response?
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(4000);
function handler (req, res) {
fs.readFile(__dirname + '/public/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {...}
index.html
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet",type="text/css" href="styles.css"/>
<script type="text/javascript" src="node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js">></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>
<body>
<script>
var socket = io.connect('http://localhost:4000');
socket.on('connect', function(){...}
Have you tried referencing socket.io.js like this?
<script src="/socket.io/socket.io.js"></script>
In your handler function, you appear to be responding to all requests by providing index.html back to the user. This is why your CSS stylesheet and Javascript files are not being returned.
I am having some problems in executing my first socket.io program. Here is the code
var io=require('socket.io');
var http=require('http');
server =http.createServer(function(req,res) {
res.writeHead(200,{'Content-Type':'text/html'});
res.write('welcome');
res.end();
});
server.listen(7777);
var socket = io.listen(server);
socket.on('connection', function (client) {
console.log('client connected');
client.emit('news', { hello: 'world' });
client.on('another', function (data) {
console.log(data);
});
});
And here is my client side code
<html>
<head>
<script type="text/javascript" src="http://localhost:7777/socket.io/lib/socket.io.js"> </script>
<script type="text/javascript">
var socket = io.connect("http://localhost:7777");
socket.on('news',function(data) {
alert(data);
socket.emit('another',{my:'data'});
});
</script>
</head>
</html>
EDIT: Now, i get an error in my server terminal "info - client protocol unsupported"
what am i doing wrong here? Thanks
You are trying to import a javascript file from the socket io port instead of the web server port.
<script type="text/javascript" src="http://localhost:7777/socket.io/lib/socket.io.js"> </script>
You need to give the correct src parameter. For newer version of socket io, this would be something like socket/socket.io.js.