我收到错误消息:
端口错误:无法建立连接。接收端不存在。
但我不知道如何解决这个问题,有什么帮助吗?这是我的代码:
服务器 (app.js)
var app = require('express')(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(8088);
//routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('set nickname', function (name) {
socket.set('nickname', name, function () { socket.emit('ready'); });
});
socket.on('msg', function () {
socket.get('nickname', function (err, name) {
console.log('Chat message by ', name);
});
});
});
客户端 (index.html)
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8088');
socket.on('news', function (data) {
console.log(data);
});
socket.on('connect', function () {
socket.emit('set nickname', prompt('What is your nickname?'));
socket.on('ready', function () {
console.log('Connected !');
socket.emit('msg', prompt('What is your message?'));
});
});
</script>
编辑
我socket.connect()
从客户端中删除但仍然收到相同的错误