Reputation: 717
I have spent literally all day visiting tutorial websites explaining how to use nodejs and sockoets.io but I'm not able to get anything to work.
I have managed to at least run a js file:
node filename.js
But it doesnt fully work. It runs until it reaches the "var server = net..." line since the "console.log("hello")" line DOES NOT EXECUTE:
var net = require('net');
var server = net.createServer(function (socket) {
console.log("hello");
socket.write('Echo server\r\n');
socket.pipe(socket);
});
console.log("hello");
server.listen(1337, '127.0.0.1');
This i got from the official node.js site home page: http://nodejs.org/
All tutorials claim that its just so easy.
I have just tried to follow this tutorial to the letter although a lot of them skim over the part I'm stuck with (the actual installing): http://tutorialzine.com/2012/08/nodejs-drawing-game/
so following the above tutorial i run app.js from the console and i get a message "socket.io started", I get stuck at the part where it asks for you to go to this URL:
http://localhost:8080
The browser attempts to go there but it hangs for a few minutes then says: "No data received Unable to load the webpage because the server sent no data."
I have no idea how node.js works and there don't seem to be explanations as to how it works... Where is node.js installed? If its meant to be on the server, how does it get installed on the server? where should I install it to test locally? what is socket.io? where should that be installed?
All I seem to get on node.js info sites are code block dumps with little explanation as what is going on.
I followed a youtube tutorial where the guy was using WAMP server, so I thought maybe I needed to put files on a server, so I installed WAMP and disabled IIS8 server. Another note, when going to "localhost" on my browser it says "it works!" which seems like an automating message from a local server - I thought it was IIS8 but even though I disable the service, that message displays. Even if I install WAMP and have it running that message displays. Also, WAMP doesn't work either, since php files don't run. Localhost always takes me to a page displaying that message.
Is this a local server issue?
Upvotes: 0
Views: 1656
Reputation: 19591
I have created a basic gist on github for using socket.io + node + express
The minimum working environment for making socket.io app is this :
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server);
app.get('/', function(req, res) {
res.send('<!doctype html> \
<html> \
<head><meta charset="utf-8"></head> \
<body> \
<center>Welcome to <strong>socket.io</strong></center> \
<script src="/socket.io/socket.io.js"></script> \
<script> \
var socket = io.connect(); \
socket.emit("message", "Howdy"); \
setInterval(function () { \
socket.emit("message", "Ping"); \
}, 1000); \
</script> \
</body> \
</html>');
});
io.sockets.on('connection', function (socket) {
socket.on('message', function(msg) {
console.log(msg);
});
});
server.listen(8000);
Upvotes: 3
Reputation: 7666
you need to require('socket.io') and then create a connection io.sockets.on('connection', function (socket) in order to make it work
Upvotes: 0
Reputation: 11712
it is hard to give an "answer" to your question(s). I would recommend you start with a much more basic introduciton that the drawing game. Also, I would suggest you start with nodejs as is, without using socket.io right away. when you understand how node works, you can start with websockets.
Here is some node 101 stuff:
You should not need WAMP at all. nodjs is the server!
It seems that you have no idea what ports are. Your node script start a webserver that listens on port 1337. If you want to see what that server serves, you need to point your browser to localhost:1337
(not port 8080, as you tried)
Upvotes: 3