我收到错误一个简单的socket:试图创建使用socket.io
Port error: Could not establish connection. Receiving end does not exist.
,但我无法弄清楚如何解决这一问题,任何帮助吗?这里是我的代码:
服务器(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()
从客户端,但仍然得到同样的错误
即使没有socket.connect();我得到那个错误,我想我错过了别的东西。另一件事是,我花了很多时间来得到任何消息,我认为这是socket.io的一个已知问题 – Tsundoku