开发者

Where can I find a nodejs/javascript client combination example, showing data being loaded in the client from the server, modified, and sent back?

开发者 https://www.devze.com 2023-03-06 06:32 出处:网络
Any working example, using the latest version of nodej开发者_JAVA百科s, will work; ideally, it\'s as simple as possible. to use create a folder, npm install express socket.io then place in the three f

Any working example, using the latest version of nodej开发者_JAVA百科s, will work; ideally, it's as simple as possible.


to use create a folder, npm install express socket.io then place in the three files, and 'node app.js'.

layout.jade

!!! 5
title=title
body!=body

index.jade

script(src='http://cdn.socket.io/stable/socket.io.js')
script
  //create socket
  var socket = new io.Socket();
  //connect socket
  socket.connect();
  //on data recieved
  socket.on('message', function(data){
    //log data
    console.log( data );
    //modify data
    data.modified = true;
    //return data
    socket.send(data);
  });

app.js

// expressjs.com, a web framework
var express = require('express');
// socket.io, real time communications
var io = require('socket.io');


//create web server
var app = module.exports = express.createServer();

//configure web server
app.configure( function () {
  app.set('views', __dirname);
  app.set('view engine', 'jade');
  app.use(app.router);
});
//handle requests for /
app.get('/', function (req, res, next) {
  res.render('index', {
    title: 'socket.io test'
  });
});

// listen on port 8080
app.listen( 8080 );
console.log("Express server listening on port %d", app.address().port);

// attach socket.io to the web server
var socket = io.listen( app ); 

// when a client connects
socket.on('connection', function(client){ 
  //send the client some data
  client.send({ data: [1,2,3] });
  // when the client sends back data
  client.on('message', function (msg){
    // log the data
    console.log( msg );
  }); 
});


Maybe, nowjs is that you want. It's provide simple API to call server functions from client.

0

精彩评论

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