在哪里可以找到nodejs/javascript客户端组合示例,显示从服务器加载到客户端、修改并发回的数据?

时间:2022-10-06 22:52:44

Any working example, using the latest version of nodejs, will work; ideally, it's as simple as possible.

任何使用最新版本的nodejs的工作示例都将有效;理想情况下,它尽可能简单。

2 个解决方案

#1


2  

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

要使用创建文件夹,npm安装express socket。然后将io放入三个文件中,并将其命名为“node app.js”。

layout.jade

layout.jade

!!! 5
title=title
body!=body

index.jade

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

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 );
  }); 
});

#2


2  

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

也许,现在是你想要的。它提供了从客户端调用服务器函数的简单API。

#1


2  

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

要使用创建文件夹,npm安装express socket。然后将io放入三个文件中,并将其命名为“node app.js”。

layout.jade

layout.jade

!!! 5
title=title
body!=body

index.jade

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

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 );
  }); 
});

#2


2  

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

也许,现在是你想要的。它提供了从客户端调用服务器函数的简单API。