我正在使用socket.io创建一个消息页面。在它的键入部分,我收到一条错误消息,指出“ TypeError:sendTypingFromUser不是函数”。我已经分配了全局变量“ sendTypingFromUser” 到在模块.exports对象外部创建的函数“ sendTypingToChat”。一旦连接了socket.io,就应该将该函数分配给变量。在socket.on('USER_CONNECTED')中,我已将该变量分配给该函数。然后,我在socket.on(“ TYPING”)下调用了“ sendTypingFromUser”。
这是我的代码:
const io = require('./index.js').io
const { VERIFY_USER, USER_CONNECTED, USER_DISCONNECTED,
LOGOUT, COMMUNITY_CHAT, MESSAGE_RECIEVED, MESSAGE_SENT,
TYPING, PRIVATE_MESSAGE } = require('../components/Events')
const { createUser, createMessage, createChat } = require('../components/Factories')
let connectedUsers = { }
let communityChat = createChat()
module.exports = function(socket){
// console.log('\x1bc'); //clears console
console.log("Socket Id:" + socket.id);
var sendMessageToChatFromUser;
var sendTypingFromUser;
//Verify Username
// socket.on(VERIFY_USER, (nickname, callback)=>{
// if(isUser(connectedUsers, nickname)){
// callback({ isUser:true, user:null })
// }else{
// callback({ isUser:false, user:createUser({name:nickname, socketID:socket.id})})
// }
// })
//User Connects with username
socket.on(USER_CONNECTED, (user)=>{
console.log(user);
user.socketID = socket.id
connectedUsers = addUser(connectedUsers, user)
socket.user = user
sendMessageToChatFromUser = sendMessageToChat(user.name)
sendTypingFromUser = sendTypingToChat(user.name)
io.emit(USER_CONNECTED, connectedUsers)
console.log(connectedUsers);
})
//User disconnects
socket.on('disconnect', ()=>{
if("user" in socket){
connectedUsers = removeUser(connectedUsers, socket.user.name)
io.emit(USER_DISCONNECTED, connectedUsers)
console.log("Disconnect", connectedUsers);
}
})
//User logsout
socket.on(LOGOUT, ()=>{
connectedUsers = removeUser(connectedUsers, socket.user.name)
io.emit(USER_DISCONNECTED, connectedUsers)
console.log("Disconnect", connectedUsers);
})
//Get Community Chat
socket.on(COMMUNITY_CHAT, (callback)=>{
callback(communityChat)
})
socket.on(MESSAGE_SENT, ({chatId, message})=>{
sendMessageToChatFromUser(chatId, message)
})
socket.on(TYPING, ({chatId, isTyping})=>{
sendTypingFromUser(chatId, isTyping)
})
socket.on(PRIVATE_MESSAGE, ({receiver, sender}) => {
if(receiver in connectedUsers) {
const newChat = createChat({ name: `${receiver}&${sender}`, users: [receiver, sender] })
const receiverSocket = connectedUsers[receiver].socketID
socket.to(receiverSocket).emit(PRIVATE_MESSAGE, newChat)
socket.emit(PRIVATE_MESSAGE, newChat)
}
})
}
/*
* Returns a function that will take a chat id and a boolean isTyping
* and then emit a broadcast to the chat id that the sender is typing
* @param sender {string} username of sender
* @return function(chatId, message)
*/
function sendTypingToChat(user){
return (chatId, isTyping)=>{
io.emit(`${TYPING}-${chatId}`, {user, isTyping})
}
}
/*
* Returns a function that will take a chat id and message
* and then emit a broadcast to the chat id.
* @param sender {string} username of sender
* @return function(chatId, message)
*/
function sendMessageToChat(sender){
return (chatId, message)=>{
io.emit(`${MESSAGE_RECIEVED}-${chatId}`, createMessage({message, sender}))
}
}
/*
* Adds user to list passed in.
* @param userList {Object} Object with key value pairs of users
* @param user {User} the user to added to the list.
* @return userList {Object} Object with key value pairs of Users
*/
function addUser(userList, user){
let newList = Object.assign({}, userList)
newList[user.name] = user
return newList
}
/*
* Removes user from the list passed in.
* @param userList {Object} Object with key value pairs of Users
* @param username {string} name of user to be removed
* @return userList {Object} Object with key value pairs of Users
*/
function removeUser(userList, username){
let newList = Object.assign({}, userList)
delete newList[username]
return newList
}
/*
* Checks if the user is in list passed in.
* @param userList {Object} Object with key value pairs of Users
* @param username {String}
* @return userList {Object} Object with key value pairs of Users
*/
function isUser(userList, username){
return username in userList
}