这是我的server.js代码:
var express =require('express'),
http = require('http');
var app= express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
users = [];
connections = [];
server.listen(process.env.Port || 3000);
console.log('server running ...')
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
io.sockets.on('connection', function(socket){
connections.push(socket);
console.log('Connected: %s sockets connected', connections.length);
socket.on('disconnect',function(data){
users.splice(users.indexOf(socket.username),1);
updateUsernames();
connections.splice(connections.indexOf(socket),1);
console.log('Disconnected: %s sockets conected', connections.length);
});
//send message
socket.on('send message', function(data){
console.log(data)
io.sockets.emit('new message',{msg: data, user: socket.username});
});
//new User
socket.on('new user', function(data,callback){
callback(true);
socket.username = data;
users.push(socket.username);
updateUsernames();
});
function updateUsernames(){
io.sockets.emit('get users', users)
}
});
这是我的html文件:
<!DOCTYPE html>
<html>
<head>
<title>IO chat</title>
<link rel = "stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<style >
body{
margin-top: 30px;
}
#messageArea{
display: none;
}
</style>
</head>
<body>
<div class = "container">
<div id = "userFormArea" class= "row">
<div class = "col-md-12">
<form id = "userForm">
<div class = "form-group">
<label>Enter Username</label>
<input class = "form-control" id = "username">
<br/>
<input type="submit" class = "btn btn-primary" value = " Login"/>
</div>
</form>
</div>
</div>
<div class = "row" id = "messageArea">
<div class = "col-md-4">
<div class = "well">
<h3> Online users </h3>
<ul class = "list-group" id = "users"> </ul>
</div>
</div>
<div class = "col-md-8">
<div class="chat" id = "chat"> </div>
<form id = "messageForm">
<div class = "form-group">
<label>Enter Message</label>
<textarea class = "form-control" id = "message"> </textarea>
<br/>
<input type="submit" class = "btn btn-primary" value = "Send Message"/>
</div>
</form>
</div>
</div>
</div>
<script>
$(function(){
var socket = io.connect();
var $messageForm = $('#messageForm');
var $message = $('#message');
var $chat = $('#chat');
var $userFormArea = $('#userFormArea');
var $userForm = $('#userForm');
var $messageArea = $('#messageArea');
var $users = $('#users');
var $username = $('#username');
$messageForm.submit(function(e){
e.preventDefault();
socket.emit('send message', $message.val());
$message.val('');
});
socket.on('new message', function(data){
$chat.append('<div class = "well"><strong>'+data.user+'</strong>:' + data.msg + '<div>');
});
$userForm.submit(function(e){
e.preventDefault();
socket.emit('new user', $username.val(),function(data){
if(data){
$userFormArea.hide();
$messageArea.show();
}
});
$username.val('');
});
socket.on('get users', function(data){
var html = '';
for (i = 0; i< data.length; i++){
html +='<li class = "list-group-item" >' + data[i]+ '</li>';
}
$users.html(html);
});
});
</script>
</body>
</html>
我一直在努力争取这个问题,socket.io有问题吗?或者是我做错了。
如果我有什么不同,请告诉我。我确实连接到套接字,它会喷出控制台日志。
正如你在图片中看到的,我希望它说连接:在一个人连接后连接1个插座。
答案 0 :(得分:0)
通常在客户端和服务器使用不同版本的socket.io
时发生。从package.json
检查服务器的版本,并为客户端提供相同的版本。我遇到了同样的问题,并升级到socket.io version 2.1.1
来解决。