我得到错误socket.io.js:2919和net :: err_connection_refused 只是希望得到解决,谢谢 需要添加更多详细信息才能发布此
继承服务器套接字代码
var app = require('http').createServer(connectHandler),
io = require('socket.io').listen(app),
fs = require('fs');
app.listen(8080);
function connectHandler (req, res) {
fs.readFile(__dirname + '/client.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading client.html');
}
res.writeHead(200);
res.end(data);
});
}
var members = [];
io.sockets.on('connection', function (socket) {
socket.on('joined', function(data) {
var mbr = data;
mbr.id = socket.id;
members.push(mbr);
socket.broadcast.emit('joined', data);
console.log(data.name, 'joined the room');
});
socket.on('message', function(data) {
socket.broadcast.emit('message', data);
});
socket.on('disconnect', function() {
for (var i = 0; i < members.length; i++) {
if (members[i].id === socket.id) {
socket.broadcast.emit('disconnected', { name: members[i].name });
}
}
});
});
这里是html套接字客户端
<!doctype html>
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<div id="messages">
</div>
<form id="newChat">
<textarea id="text"></textarea>
<input type="submit" id="sendMessage" value="Send" />
</form>
<script>
var socket = io.connect('http://localhost');
var who;
socket.on('connect', function() {
var chatter = prompt('Please enter your name');
chatter = (chatter === "" || chatter === null) ? "anon" : chatter;
addChatter("you", "Joined");
who = chatter;
socket.emit('joined', {
name: chatter
});
});
function addChatter(name, message) {
var chat = document.getElementById("messages");
chat.innerHTML += "<div>" + name + " - " + message + "</div>";
}
socket.on('joined', function(data) {
console.log(data);
addChatter(data.name, ' joined');
});
socket.on('disconnected', function(data) {
addChatter(data.name, 'disconnected');
});
答案 0 :(得分:2)
html文件中的localhost连接可以通过以下方式在新版本中建立:
var socket = io();
将自行绑定到服务器。
请参阅http://socket.io/get-started/chat/
作为旁注;在服务器上启动连接可以通过以下方式完成:
io.on('connection' ...
而不是
io.sockets.on('connection' ...
var app = require('http').createServer(connectHandler),
io = require('socket.io').listen(app),
fs = require('fs');
function connectHandler (req, res) {
fs.readFile(__dirname + '/client.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading client.html');
}
res.writeHead(200);
res.end(data);
});
}
var members = [];
io.on('connection', function (socket) {
console.log(socket);
socket.on('joined', function(data) {
var mbr = data;
mbr.id = socket.id;
members.push(mbr);
socket.broadcast.emit('joined', data);
console.log(data.name, 'joined the room');
});
socket.on('message', function(data) {
socket.broadcast.emit('message', data);
});
socket.on('disconnect', function() {
for (var i = 0; i < members.length; i++) {
if (members[i].id === socket.id) {
socket.broadcast.emit('disconnected', { name: members[i].name });
}
}
});
});
app.listen(8077);
&#13;
<!doctype html>
<html>
<head>
<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
</head>
<body>
<div id="messages">
</div>
<form id="newChat">
<textarea id="text"></textarea>
<input type="submit" id="sendMessage" value="Send" />
</form>
<script type="text/javascript">
var socket = io();
var who;
socket.on('connect', function() {
var chatter = prompt('Please enter your name');
chatter = (chatter === "" || chatter === null) ? "anon" : chatter;
addChatter("you", "Joined");
who = chatter;
socket.emit('joined', {
name: chatter
});
});
function addChatter(name, message) {
var chat = document.getElementById("messages");
chat.innerHTML += "<div>" + name + " - " + message + "</div>";
}
socket.on('joined', function(data) {
console.log(data);
addChatter(data.name, ' joined');
});
socket.on('disconnected', function(data) {
addChatter(data.name, 'disconnected');
});
</script>
&#13;