簡體   English   中英

NodeJS 和 Socket.io

[英]NodeJS and Socket.io

我剛剛安裝了這些 nodejs 和 socket.io 但是我在讓客戶端連接到服務器時遇到了問題。

在我的服務器中,我有:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(3000, 'localhost');
console.log('Server running at http://localhost:3000/');

var io = require('socket.io').listen(3000);

io.sockets.on('connection', function (socket) {

  socket.emit('Hi.') ;

});

在我的客戶上:

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:3000');

socket.on('connect', function () {
  socket.emit('set nickname', confirm('What is your nickname?'));
  socket.on('ready', function () {
    console.log('Connected !');
    socket.emit('msg', confirm('What is your message?'));
  });
});

我在 Chrome 檢查器中收到一些錯誤:

GET http://localhost:9261/socket.io/socket.io.js 404 (Not Found)
Uncaught ReferenceError: io is not defined

似乎您只向客戶端發送“Hello World”,而不是帶有客戶端代碼的 html。

您的服務器代碼應如下所示(來自http://socket.io/#how-to-use ):

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(80);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

您的客戶端代碼應該在index.html中。 還要確保您有一個名為socket.io的文件夾,其中包含腳本socket.io.js

您不是通過 nodeJS 為客戶端提供服務,這將不起作用:

<script src="/socket.io/socket.io.js"></script>

嘗試改用這個:

<script src="http://localhost:3000/socket.io/socket.io.js"></script>

它無法連接,因為您的客戶端無法找到 socket.io.js 您可以通過使用適當的文件來解決,從 node_modules\socket.io\node_modules\socket.io-client\dist 目錄使用 socket.io.js 並將其放入您的本地目錄並在腳本標記中使用它。

<script src="http://localhost:3000/socket.io/socket.io.js"></script>var socket = io.connect("http://localhost:3000");

對於這種方式,客戶端連接到節點服務器。這將起作用

另請注意,當您轉向生產時,應更新客戶端中的代碼:

var socket = io.connect('http://localhost:3000');

到類似的東西

var socket = io.connect('my_host');

我已經通過提取參數req.headers.hostmy_host發送到客戶端腳本,其中包含我正在加載的頁面socket.io ,所以對我來說它看起來像:

 io.connect('http://' + host)

您可以通過express-expose在客戶端公開變量。

這可能是因為io沒有正確實例化,因此Uncaught ReferenceError: io is not defined

// Instantiate without argument
var io = require('socket.io')();
// Instantiate with new keyword
var Server = require('socket.io')
  , io = new Server();

// Instantiate io server with app as argument
var app = require('http').createServer(handlerFunc)
  , io = require('socket.io')(app);
app.listen(80);

// Attaching socket.io with express server
var app = express()
  , server = require('http').createServer(app)
  , io = require('socket.io')(server);
server.listen(80, handlerFunc);

暫無
暫無

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

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