开发者

nodejs where to start?

I've installed nodejs and ran couple of simple examples like opening a server on a port and listen on that port.

However, I still can not relate nodejs to web development. so to learn and implement nodejs I'm thinking about making tic tac toe using rails and nodejs. Is this possible?

I'm envisioning a multi-player tic-tac-toe game where if person 1 开发者_StackOverflow社区selects something, person 2 automatically sees it on their browser.

where should I start with something like this?

I have experience in rails but none in nodejs or nodejs + rails.


First Just try to implement basic application and get a feel of the framework. There are several tutorials online for example:

http://net.tutsplus.com/tutorials/javascript-ajax/learning-serverside-javascript-with-node-js/

Documentation at http://nodejs.org/

http://howtonode.org/

Also there is a 70 min screencast by peepcode which costs 9$

https://peepcode.com/products/nodejs-i

There are also several apps on github, which you take a look at. Reading code is always the best.


I'd recommend looking at the project Socket.IO and Socket.IO-node. It uses HTML5 WebSockets if available, and falls back automatically and gracefully (no intervention required) to Flash sockets and XHR-polling as necessary

Here's a script to download the files:

mkdir socket.io
cd socket.io
git clone https://github.com/LearnBoost/Socket.IO.git --recursive
git clone https://github.com/LearnBoost/Socket.IO-node.git --recursive

Here's the server.js file:

var http=require('http');
var url=require('url');
var fs=require('fs');
var sys=require('sys');
var io=require('./socket.io/Socket.IO-node');   //adjust path as necessary...

var server=http.createServer(function(req,res){
    res.writeHead(200,{'Content-Type':'text/html'});
    res.write('Hello world');
    res.end();
});
server.listen(8000);

var socket=io.listen(server);

socket.on('connection', function(client){
  onConnection(client);
  client.on('message', function(){
    onMessage();
  })
  client.on('disconnect', function(){
    onDisconnect();
  })
});
function onConnection(client){
  console.log('connection');
  //client.connected;   //tests if connected
  //client.send("message");
  //client.broadcast("message");   //send to all other conns
}
function onMessage(){
  console.log('message');
}
function onDisconnect(){
  console.log('disconnect');
}

});

Run the above server with sudo node server.js

And here is your index.html to be run in a browser:

<script src="./socket.io/Socket.IO/socket.io.js" type="text/javascript" charset="utf-8"></script>   <!--Adjust path as necessary-->
<script>
var host="localhost";
var port=8000;

var socket=new io.Socket(host,{'port':port});

socket.connect();
socket.on('connect',function(){onConnect();}) 
socket.on('message',function(data){onMessage(data);}) 
socket.on('disconnect',function(){onDisconnect();});

function onConnect(){
  ///alert('connect');
}
function onMessage(data){
  //alert('message');
}
function onDisconnect(){
  //alert('disconnect');
  socket.connect();
}

</script>


I found The Node Beginner Book helpful. It's probably too basic for what you're looking to do, however.


I find hexes example helpful as reference for a realtime game using nodejs.

https://github.com/joyent/node-camp/tree/master/hexes

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜