无法在套接字聊天室内发送消息

时间:2017-04-19 08:45:14

标签: javascript node.js socket.io

当我尝试在房间内发送消息时,我得到TypeError: Cannot read property roomClients' of undefined, node.js和使用套接字对我来说是新的,所以修复可能很简单。

知道导致错误的原因吗?我非常感谢任何建议

这是服务器端代码:



// Setting up the server
var express = require('express');
var app = express();
var path = require('path');
var server = require('http').createServer(app);
var socket = require("socket.io").listen(server);
var Room = require('./room.js');
var _ = require('underscore')._;
var uuid = require ('uuid');
server.listen(process.env.PORT || 3000);
console.log('Server is running...');

socket.set("log level", 1);
var people = {};  
var rooms = {};
var clients = [];

Array.prototype.contains = function(k, callback) {  
    var self = this;
    return (function check(i) {
        if (i >= self.length) {
            return callback(false);
        }
        if (self[i] === k) {
            return callback(true);
        }
        return process.nextTick(check.bind(null, i+1));
    }(0));
};



// Gets the html file
app.get('/', function(req, res){
	res.sendFile(__dirname + '/index.html');
})

// Gets the css file
app.use(express.static(path.join(__dirname, 'public')));

// When connecting
socket.on("connection", function(client) {
	client.on("join", function(name){
		console.log("Someone joined the chat", name);
		roomID = null;
		people[client.id] = {"name" : name, "room" : roomID};
		sizePeople = _.size(people);
		sizeRooms = _.size(rooms);
		socket.sockets.emit("update-people", {people: people, count: sizePeople});
		socket.sockets.emit("roomList", {rooms: rooms, count: sizeRooms});
		client.emit("updateToSelf", "You have connected to the server. Join or create room to chat");
		

		client.broadcast.emit('updateToOthers', name + " is online.");
		//client.emit("roomList", {rooms: rooms});
		clients.push(client); //populate the clients array with the client object
	});

//Creating room
	client.on("serverCreateNewRoom", function(name){
		console.log("About to create a room", name)
		if (people[client.id].room === null) {
			console.log("First time creating a room")
			var id = uuid.v4();
			var room = new Room(name, id, client.id);
			rooms[id] = room;
			sizeRooms = _.size(rooms);
			socket.sockets.emit("roomList", {rooms: rooms, count: sizeRooms}); //update the list of rooms on the frontend
			client.room = name; //name the room
			client.join(client.room); //auto-join the creator to the room
			room.addPerson(client.id); //also add the person to the room object
			people[client.id].room = id; //Update the room key with the ID of the created room
			client.emit("updateToSelf", "You have created a room");
		} else {
			socket.sockets.emit("updateToSelf", "You have already created a room");
		}
	});

//joining room
	client.on("joinRoom", function(id) {
		console.log("Someone is trying to join a room")
		var room = rooms[id];
		if (client.id === room.owner) {
			client.emit("updateToSelf", "You are the owner of this room and you have already been joined.");
			console.log("Owner is trying to join own room");
		} else {
			room.people.contains(client.id, function(found) {
				if (found) {
					client.emit("updateToSelf", "You have already joined this room.");
				} else {
					if (people[client.id].inroom !== null) { //make sure that one person joins one room at a time
						client.emit("updateToSelf", "You are already in a room ("+rooms[people[client.id].inroom].name+"), please leave it first to join another room.");
						console.log("User is already in the room");
					} else {
						room.addPerson(client.id);
						people[client.id].inroom = id;
						client.room = room.name;
						client.join(client.room); //add person to the room
						user = people[client.id];
						socket.sockets.in(client.room).emit("updateToOthers", user.name + " has connected to " + room.name + " room.");
						client.emit("update", "Welcome to " + room.name + ".");
 						client.emit("sendRoomID", {id: id});
					}
				}
			});
		}
	});




// When sending
	client.on("send message", function(msg){
		if (socket.sockets.manager.roomClients[socket.id]['/'+socket.room] !== undefined) {
			socket.sockets.in(client.room).emit("chat", people[client.id], msg);
			//client.emit("ownMessage", msg);
			//client.broadcast.emit('newMessageToOthers', people[client.id], msg);
		console.log("Message send", msg);
		} else {
			client("updateToSelf", "Please connect to a room");
		}		
	});



});




0 个答案:

没有答案