Socket.io TypeError: undefined is not a function
Socket.io TypeError: undefined is not a function
我正在制作一个简单的聊天应用程序。连接、“loggin in”和发送聊天消息都很好。但是,当我刷新页面并尝试再次登录时,出现错误;
Missing error handler on socket
.
TypeError: undefined is not a function at Socket. '<'anonymous>
[ . . . ] at server.js:30:19
Server.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var users = [];
var chat_history = [];
[...]
io.on('connection', function(socket){
//
// LOGIN
///////////////////////////
socket.on('login', function(username) {
if(!socket.username && username) {
socket.emit('logged_in', 'successful');
socket.emit('chat_history', JSON.stringify(chat_history));
io.emit('notice', username + ' has connected.');
socket.username = username;
users.push(username);
update_users();
//
// CHAT MESSAGE RECEIVED
///////////////////////////
socket.on('chat_message', function(msg){
var message = getTime() + ' ' + socket.username + ': ' + msg;
update_chat_history(message);
});
//
// DISCONNECT
///////////////////////////
socket.on('disconnect', function () {
io.emit('notice', socket.username + ' has left the channel.');
console.log(socket.username + ' disconnected');
update_users();
});
} else {
socket.emit('logged_in', 'unsuccessful');
socket.emit('notice', 'Login was unsuccessful, please refresh the page and try again.');
}
});
///////////////////////////////////////////
//socket.on('error', function (err){
// console.error(err.stack);
//});
///////////////////////////////////////////
});
http.listen(port, ip);
console.log('listening on ' + ip + ':' + port);
//
// Functions
///////////////////////////
function getTime() {
var t = new Date();
t = '[' + t.getUTCHours() + ' : ' + t.getUTCMinutes() + ']';
return t;
}
function update_users() {
//console.log(io.sockets.connected);
users = [];
for( var client in io.sockets.connected ){
var username = io.sockets.connected[client].username;
if(username){
users.push(username);
}
}
users = JSON.stringify(users);
io.emit('users_online', users);
}
function update_chat_history(message_to_add){
chat_history.push(message_to_add);
if(chat_history.length > 20){
chat_history.splice(0, 1);
}
}
在socket.on('login')我测试了socket在刷新页面或断开连接后没有未定义。
我想这与它不更新连接并尝试重新连接到相同的连接有关,但我不知道如何或什么。在 Whosebug 找不到解决方案。感谢帮助。
UPDATE: Blaargh this just doesnt make any at all (╯°□°)╯︵ ┻━┻
Still the same error. now at server.js:78:11
Removed all socket.usernames and changed login to;
socket.on('login', function(username) {
if( username ) { // TODO what if duplicate username
socket.emit('logged_in', 'successful');
socket.emit('chat_history', JSON.stringify(chat_history));
io.emit('notice', username + ' has connected.');
add_user(socket.id, username);
[...]
Also changed the update_users()
function add_user(id, username){
users.push({
'id' : id,
'username' : username
});
var json_users = JSON.stringify(users);
io.emit('users_online', json_users);
}
function remove_user(id){
for(user in users){
if(users[user]['id'] == id){
users.splice(users[user], 1);
break;
}
}
var json_users = JSON.stringify(users);
io.emit('users_online', json_users);
}
UPDATE: 2
I still have no clue at all what could have been the problem however somehow I managed to write around it so its working now.
每次刷新页面时,都会删除您的套接字并创建一个新套接字(没有 属性 用户名,因为它是一个新套接字)所以您需要找到一种方法来保留您的用户名
对于你的情况,我建议你将用户名放在 cookie 中,并检查页面加载是否 cookie "username" 存在并且有价值,如果它存在然后更新你的(新)套接字
更高级的方法: handle browser reload socket io
希望这对您有所帮助!
编辑
npm install --save lodash
var _ = require('lodash')
var users = [];
io.sockets.on('connection', function (socket) {
socket.username ="guest"
socket.on('login', function(username) {
if( username) {
socket.emit('logged_in', 'successful');
io.emit('notice', username + ' has connected.');
socket.username = username ;
// users.push(username);
add_user(socket.id, username , users);
//
// CHAT MESSAGE RECEIVED
///////////////////////////
socket.on('chat_message', function(msg){
var message = date.Now() + ' ' + socket.username + ': ' + msg;
});
//
// DISCONNECT
///////////////////////////
socket.on('disconnect', function () {
io.emit('notice', socket.username + ' has left the channel.');
console.log(socket.username + ' disconnected');
});
} else {
socket.emit('logged_in', 'unsuccessful');
socket.emit('notice', 'Login was unsuccessful, please refresh the page and try again.');
}
});
socket.emit('message', { message: 'welcome to the chat' });
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
});
console.log("Listening on port " + port);
function add_user(id, username , users){
var item = {
'id' : id,
'username' : username
}
var oldItem = _.find(users, {id: id});
var index = users.indexOf(oldItem);
if (oldItem) {
users.splice(index, 1);
users.unshift(item);
} else {
users.unshift(item);
}
users = JSON.stringify(users);
console.log(users)
io.emit('users_online', users);
}
function remove_user(id){
for(user in users){
if(users[user]['id'] == id){
users.splice(users[user], 1);
break;
}
}
users = JSON.stringify(users);
io.emit('users_online', users);
}
我正在制作一个简单的聊天应用程序。连接、“loggin in”和发送聊天消息都很好。但是,当我刷新页面并尝试再次登录时,出现错误;
Missing error handler on
socket
.
TypeError: undefined is not a function at Socket. '<'anonymous>
[ . . . ] at server.js:30:19
Server.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var users = [];
var chat_history = [];
[...]
io.on('connection', function(socket){
//
// LOGIN
///////////////////////////
socket.on('login', function(username) {
if(!socket.username && username) {
socket.emit('logged_in', 'successful');
socket.emit('chat_history', JSON.stringify(chat_history));
io.emit('notice', username + ' has connected.');
socket.username = username;
users.push(username);
update_users();
//
// CHAT MESSAGE RECEIVED
///////////////////////////
socket.on('chat_message', function(msg){
var message = getTime() + ' ' + socket.username + ': ' + msg;
update_chat_history(message);
});
//
// DISCONNECT
///////////////////////////
socket.on('disconnect', function () {
io.emit('notice', socket.username + ' has left the channel.');
console.log(socket.username + ' disconnected');
update_users();
});
} else {
socket.emit('logged_in', 'unsuccessful');
socket.emit('notice', 'Login was unsuccessful, please refresh the page and try again.');
}
});
///////////////////////////////////////////
//socket.on('error', function (err){
// console.error(err.stack);
//});
///////////////////////////////////////////
});
http.listen(port, ip);
console.log('listening on ' + ip + ':' + port);
//
// Functions
///////////////////////////
function getTime() {
var t = new Date();
t = '[' + t.getUTCHours() + ' : ' + t.getUTCMinutes() + ']';
return t;
}
function update_users() {
//console.log(io.sockets.connected);
users = [];
for( var client in io.sockets.connected ){
var username = io.sockets.connected[client].username;
if(username){
users.push(username);
}
}
users = JSON.stringify(users);
io.emit('users_online', users);
}
function update_chat_history(message_to_add){
chat_history.push(message_to_add);
if(chat_history.length > 20){
chat_history.splice(0, 1);
}
}
在socket.on('login')我测试了socket在刷新页面或断开连接后没有未定义。
我想这与它不更新连接并尝试重新连接到相同的连接有关,但我不知道如何或什么。在 Whosebug 找不到解决方案。感谢帮助。
UPDATE: Blaargh this just doesnt make any at all (╯°□°)╯︵ ┻━┻
Still the same error. now at server.js:78:11
Removed all socket.usernames and changed login to;
socket.on('login', function(username) { if( username ) { // TODO what if duplicate username socket.emit('logged_in', 'successful'); socket.emit('chat_history', JSON.stringify(chat_history)); io.emit('notice', username + ' has connected.'); add_user(socket.id, username); [...]
Also changed the update_users()
function add_user(id, username){ users.push({ 'id' : id, 'username' : username }); var json_users = JSON.stringify(users); io.emit('users_online', json_users); } function remove_user(id){ for(user in users){ if(users[user]['id'] == id){ users.splice(users[user], 1); break; } } var json_users = JSON.stringify(users); io.emit('users_online', json_users); }
UPDATE: 2
I still have no clue at all what could have been the problem however somehow I managed to write around it so its working now.
每次刷新页面时,都会删除您的套接字并创建一个新套接字(没有 属性 用户名,因为它是一个新套接字)所以您需要找到一种方法来保留您的用户名
对于你的情况,我建议你将用户名放在 cookie 中,并检查页面加载是否 cookie "username" 存在并且有价值,如果它存在然后更新你的(新)套接字
更高级的方法: handle browser reload socket io
希望这对您有所帮助!
编辑
npm install --save lodash
var _ = require('lodash')
var users = [];
io.sockets.on('connection', function (socket) {
socket.username ="guest"
socket.on('login', function(username) {
if( username) {
socket.emit('logged_in', 'successful');
io.emit('notice', username + ' has connected.');
socket.username = username ;
// users.push(username);
add_user(socket.id, username , users);
//
// CHAT MESSAGE RECEIVED
///////////////////////////
socket.on('chat_message', function(msg){
var message = date.Now() + ' ' + socket.username + ': ' + msg;
});
//
// DISCONNECT
///////////////////////////
socket.on('disconnect', function () {
io.emit('notice', socket.username + ' has left the channel.');
console.log(socket.username + ' disconnected');
});
} else {
socket.emit('logged_in', 'unsuccessful');
socket.emit('notice', 'Login was unsuccessful, please refresh the page and try again.');
}
});
socket.emit('message', { message: 'welcome to the chat' });
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
});
console.log("Listening on port " + port);
function add_user(id, username , users){
var item = {
'id' : id,
'username' : username
}
var oldItem = _.find(users, {id: id});
var index = users.indexOf(oldItem);
if (oldItem) {
users.splice(index, 1);
users.unshift(item);
} else {
users.unshift(item);
}
users = JSON.stringify(users);
console.log(users)
io.emit('users_online', users);
}
function remove_user(id){
for(user in users){
if(users[user]['id'] == id){
users.splice(users[user], 1);
break;
}
}
users = JSON.stringify(users);
io.emit('users_online', users);
}