创建简单的节点js服务器和客户端 [英] create simple node js server and client

查看:94
本文介绍了创建简单的节点js服务器和客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是有一台服务器,其中一台客户端将发送更新,其他客户端将接收这些更新并更新页面的相关部分。



这对于节点js来说非常简单,但我不知道从哪里开始。



这里有没有人可以帮助并推动如何启动这个客户端和服务器。



非常感谢!



我已经找遍了各种各样的东西来帮助我,但他们最终都失败了....






更新



我想使用socket.io和nodeJS来建立连接。



我有

  var http = require('http'),
io = require('socket.io'),//对于npm,否则使用require('./ path / to / socket.io')

server = http.createServer(function(req,res) {
//你的普通服务器鳕鱼e
res.writeHead(200,{'Content-Type':'text / html'});
res.end('< h1> Hello world< / h1>');
});
server.listen(5454);

// socket.io
var socket = io.listen(server);
socket.on('connection',function(client){
// new client is here!
console.log('new connection!');
client.on ('message',function(){console.log('send message')})
client.on('disconnect',function(){console.log('disconnect')})
});

我也有代码给客户端。
,但它有错误:

 < script src =http://cdn.socket.io/stable /socket.io.js\"></script> 
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js>< / script>
< script>
var socket = new io.Socket('localhost');
socket.connect();
console.log(socket);
socket.on('connect',function(evt){console.log('connected',evt)})
socket.on('message',function(evt){console.log ''message',evt)})
socket.on('disconnect',function(evt){console.log('disconnected',evt)})
< / script>

更新2:



这是我运行服务器时发生的情况:

$ p $ [nlubin @ localhost websocket] $ sudo node server.js
10 Mar 17:40:49 - socket.io ready - 接受连接

ok和这是我在Chrome浏览器中看到的控制台,当我运行客户端时:

 意外的响应代码:301 
1299796919120Failed加载资源:服务器响应状态为404(未找到)
XHR完成加载:http://localhost/socket.io/xhr-polling//1299796919120。
1299796919120无法加载资源:服务器响应状态为404(未找到)
XHR完成加载:http://localhost/socket.io/xhr-polling//1299796919120。
1299796919130无法加载资源:服务器响应状态为404(未找到)
XHR完成加载:http://localhost/socket.io/xhr-polling//1299796919130。
1299796919130无法加载资源:服务器响应状态为404(未找到)
XHR完成加载:http://localhost/socket.io/xhr-polling//1299796919130。
1299796919151无法加载资源:服务器响应状态为404(未找到)
XHR完成加载:http://localhost/socket.io/xhr-polling//1299796919151。
1299796919157无法加载资源:服务器响应状态为404(未找到)
XHR完成加载:http://localhost/socket.io/xhr-polling//1299796919157。
1299796919162无法加载资源:服务器响应状态为404(未找到)
XHR完成加载:http://localhost/socket.io/xhr-polling//1299796919162。
1299796919166无法加载资源:服务器响应状态为404(未找到)
XHR完成加载:http://localhost/socket.io/xhr-polling//1299796919166。
1299796919170无法加载资源:服务器响应状态为404(未找到)
XHR完成加载:http://localhost/socket.io/xhr-polling//1299796919170。
1299796919174无法加载资源:服务器响应状态为404(未找到)
XHR完成加载:http://localhost/socket.io/xhr-polling//1299796919174。
1299796919177无法加载资源:服务器响应状态为404(未找到)
XHR完成加载:http://localhost/socket.io/xhr-polling//1299796919177。
1299796919181无法加载资源:服务器的状态为404(未找到)

而那些XHR错误仍在继续

解决方案

如果你想得到一个你真正需要的东西,体面的答案。



我假设你说'客户'是指一个网络浏览器。如果我正确地理解了你的话,你会希望将数据从服务器推送到多个Web浏览器客户端。在这种情况下,您希望在浏览器和服务器之间保持持续连接 - 请查看 Socket.io 。基本流程将是(1)用户访问您的网页,(2)生活在该网页上的一些JavaScript将连接到您的Node.js / Socket。 (3)服务器可以将消息推送到任何/所有当前连接的浏览器,并且浏览器可以将消息发送到服务器,(4)对于来自服务器的消息,在每个客户端上运行的Javascript可以解释这些消息并对网页的DOM进行适当的更改。






编辑:您的客户端javascript无法连接到服务器。如果它们运行在同一台计算机上( localhost ),那么可能意味着端口是问题所在。



试试为你的客户端指定'port'选项,在你的情况下'5454'。



$ p $ var socket = new io.Socket ( '本地主机',{ '端口':5454});

确保重新启动Node.js服务器并刷新网页。


What i want to be able to do is have a server which one client will send updates and multiple other clients would receive those updates and update the relevant parts of the page.

This must be really easy todo with node js, but i just dont know where to get started.

Is there anyone here that can help and give me a push of how to startup this client and server.

Thank so much!

I have looked all over for something to help me but they all end up failing....


UPDATE

I want to use socket.io and nodeJS to make the connections.

I have starter code for the server which i got online:

var http = require('http'),
    io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io')

server = http.createServer(function(req, res){
 // your normal server code
 res.writeHead(200, {'Content-Type': 'text/html'});
 res.end('<h1>Hello world</h1>');
});
server.listen(5454);

// socket.io
var socket = io.listen(server);
socket.on('connection', function(client){
  // new client is here!
  console.log('new connection!');
  client.on('message', function(){ console.log('send message') })
  client.on('disconnect', function(){ console.log('disconnect') })
});

also i have code for the client. but it has errors:

  <script src="http://cdn.socket.io/stable/socket.io.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>
 var socket = new io.Socket('localhost');
 socket.connect();
 console.log(socket);
 socket.on('connect', function(evt){ console.log('connected',evt) })
 socket.on('message', function(evt){ console.log('got message',evt) })
 socket.on('disconnect', function(evt){ console.log('disconnected',evt) })
</script>

UPDATE 2:

This is what happens when i run the server:

[nlubin@localhost websocket]$ sudo node server.js
10 Mar 17:40:49 - socket.io ready - accepting connections

ok and this is what i see in the console in chrome when i run the client:

    Unexpected response code: 301
1299796919120Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919120".
1299796919120Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919120".
1299796919130Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919130".
1299796919130Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919130".
1299796919151Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919151".
1299796919157Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919157".
1299796919162Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919162".
1299796919166Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919166".
1299796919170Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919170".
1299796919174Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919174".
1299796919177Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919177".
1299796919181Failed to load resource: the server responded with a status of 404 (Not Found)

And those XHR error keep happening

解决方案

You really need to give us more context to what you are doing if you want to get a decent an answer.

I assume when you say 'client' you are referring to a web browser. And if I understand you correctly you're going to want to be 'pushing' data from the server to several web browser clients. In which case you are going to want to keep a persistent connection open between the browser and server--check out Socket.io.

The basic flow will be (1) a user visits your webpage, (2) some javascript which lives on that webpage will connect to your Node.js/Socket.io server, (3) the server can then push messages to any/all of the currently connected browsers AND the browser can send messages to the server, (4) for messages coming from the server the Javascript running on each client can interpret those messages and make the appropriate changes to the web page's DOM.


EDIT: Your client side javascript can't connect to the server. If they are running on the same machine (localhost) then that probably means the port is the problem.

Try specifying the 'port' option for your client, which in your case is '5454'.

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

Be sure to restart the Node.js server AND refresh the webpage.

这篇关于创建简单的节点js服务器和客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆