簡體   English   中英

創建簡單的節點js服務器和客戶端

[英]create simple node js server and client

我想要做的是有一台服務器,一個客戶端將發送更新,而其他多個客戶端將接收這些更新並更新頁面的相關部分。

使用Node js確實很容易做到,但是我只是不知道從哪里開始。

這里有沒有人可以幫助您,以及如何啟動此客戶端和服務器。

非常感謝!

我四處尋找可以幫助我的東西,但它們都以失敗告終。


更新

我想使用socket.io和nodeJS進行連接。

我有上線的服務器入門代碼:

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

我也有客戶端代碼。 但是有錯誤:

  <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>

更新2:

這是我運行服務器時發生的情況:

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

好的,這是我在運行客戶端時在chrome控制台中看到的內容:

    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)

那些XHR錯誤不斷發生

如果您想得到一個體面的答案,您確實需要為我們提供更多有關您正在做的事情的背景信息。

我假設當您說“客戶”時,是指Web瀏覽器。 而且,如果我對您的理解正確,您將希望將數據從服務器“推送”到多個Web瀏覽器客戶端。 在這種情況下,您將要保持瀏覽器與服務器之間的持久連接打開-簽出Socket.io

基本流程是(1)用戶訪問您的網頁,(2)該網頁上存在的一些javascript將連接到您的Node.js / Socket.io服務器,(3)服務器然后可以將消息推送到任何/所有當前連接的瀏覽器中的一個,瀏覽器可以將消息發送到服務器,(4)對於來自服務器的消息,每個客戶端上運行的Javascript可以解釋這些消息並對網頁DOM進行適當的更改。


編輯:您的客戶端javascript無法連接到服務器。 如果它們在同一台計算機( localhost )上運行,則可能意味着端口出了問題。

嘗試為客戶端指定“端口”選項,在您的情況下為“ 5454”。

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

確保重新啟動Node.js服務器並刷新網頁。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM