嘿,我是套接字和節點的新手,並且正在使用socket.io進行聊天。Socket.io集成節點
這裏是我的index.js文件:
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res) {
//route handler serve index.html file
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket) {
console.log('A user has connected');
socket.on('disconnect', function() {
console.log("A user has disconnected");
})
})
http.listen(3000, function() {
console.log('listening on port 3000');
});
**這是我的index.html:**
<html>
<head>
<title> Chat </title>
<script src="https://cdn.socket.io/socket.io-1.0.0.js"></script>
<script>
var socket = io();
</script>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off"/> <button> Send </button>
</form>
</body>
我總是收到引用錯誤說法該io未被定義。我試圖鏈接套接字作爲一個CDN和作爲我的服務器網址。
在服務器上也需要socket.io。嘗試安裝socket-io並添加以下代碼:var io = require('socket.io')(app);'''。 –
@HimaniAgrawal謝謝,這工作! – rxa