socket.emit不工作但socket.broadcast工作正常。可能是范围错误?

时间:2017-01-30 16:35:56

标签: javascript node.js sockets socket.io

我想运行带有socket.io的Node.js服务器,目前它将数据包发送到房间中的其他客户端socket.broadcast.to(socket.room).emit('AddPlayer'...但是socket.emit(似乎没有工作,不会将内容回复给请求该事件的客户端。任何人都可以看到我可能出错的地方。我确实尝试通过查询函数传递套接字对象,但没有效果。认为套接字变量的范围是正确的。

    io.on('connection', function (socket) { // on on client connection
        socket.on('login', function (uname, pword, email) { // on event login
            if (uname != "" && pword != "" && email != "" && validateEmail(email)) { // check if username password and email are all valid inputs
                Query('SELECT ID FROM Users WHERE Active AND Username="'+uname+'" AND Password="'+pword+'" AND Email="'+email+'";', function(rows, fields){
                    if (rows.length == 1){ // check if there was a result
                        socket.id = rows[0].ID
                        Query("SELECT S.RoomID, S.Name1,S.Name2,S.Name3, S.HP,S.MaxHP,S.SP,S.MaxSP,S.XP,S.Money,S.CellX,S.CellY,L.Skin,L.HairStyle,L.HairColor,L.ShirtID,L.ShoesID,L.TrowsersID,L.WepponID,L.HeadID FROM Stats S, Look L, Items I WHERE S.ID="+socket.id+" AND L.ID=S.ID GROUP BY S.ID; SELECT Q.ID AS QuestID, Q.LevelID AS LevelRequired, Q.XP, Q.Money, CASE WHEN S.Stage=0 THEN Q.NPCID1 WHEN S.Stage=1 THEN Q.NPCID2 END as NPCID,CASE WHEN S.Stage=0 THEN Q.Text1 WHEN S.Stage=1 THEN Q.Text2 END as Text, NPCs.RoomID, NPCs.ID FROM Quests as Q, QuestStages as S, Stats as St, NPCs WHERE S.QuestID = Q.ID AND St.LevelID <= Q.LevelID AND S.UserID=St.ID AND St.ID="+socket.id+";", function(rows, fields){
                            socket.room = rows[0][0].RoomID
                            socket.join(socket.room)
                            socket.jointime = new Date();
                            socket.battle = false;
                            if (activeusers[socket.room] === undefined) activeusers[socket.room] = {}; // allocate space in active users object for players data structure
                            activeusers[socket.room][socket.id] = {Name1: rows[0][0].Name1, Name2: rows[0][0].Name2, Name3: rows[0][0].Name3,CellX: rows[0][0].CellX,CellY: rows[0][0].CellY,RoomID: rows[0][0].RoomID, ID: socket.id, Skin: rows[0][0].Skin, HairStyle:rows[0][0].HairStyle,HairColor:rows[0][0].HairColor,ShirtID:rows[0][0].ShirtID,ShoesID:rows[0][0].ShoesID,TrowsersID:rows[0][0].TrowsersID,WepponID:rows[0][0].WepponID,HeadID:rows[0][0].HeadID, Path:[], Quests:rows[1]}; // convert gathered data from database to player data structure
                            socket.emit('PlayerStructure', activeusers[socket.room][socket.id]);
                            socket.broadcast.to(socket.room).emit('AddPlayer', activeusers[socket.room][socket.id]); // same as above
                            if (activeusers[socket.room] === undefined) activeusers[socket.room] = {}; // allocate space in active users object for players data structure
                            if (activerooms[socket.room] === undefined) {
                                Query('SELECT R.Name AS RoomName, R.X AS RoomX, R.Y AS RoomY, Re.Name FROM Rooms as R, Regions as Re, Stats As S WHERE R.RegionID = Re.ID AND R.ID=S.RoomID AND S.ID = "'+socket.id +'"; SELECT M.Collumn AS X, M.Row AS Y, M.Walkable FROM Rooms as R, MatrixCells as M, Stats As S WHERE M.RoomID=R.ID AND R.ID=S.RoomID AND S.ID="'+socket.id +'"; SELECT CASE WHEN C.RoomID1=R.ID THEN C.RoomID2 WHEN C.RoomID2=R.ID THEN C.RoomID1 END as RoomID, CASE WHEN C.RoomID1=R.ID THEN C.Wall1 WHEN C.RoomID2=R.ID THEN C.Wall2 END as Wall, C.Point, C.Battle FROM Rooms as R, Connections as C, Stats as S WHERE (C.RoomID1=R.ID OR C.RoomID2=R.ID) AND R.ID=S.RoomID AND S.ID="'+socket.id +'";', function(rows, fields){
                                    activerooms[socket.room]= GetRoom(rows);
                                    socket.emit('RoomStructure', activerooms[socket.room]);
                                });
                            } else {
                                socket.emit('RoomStructure', activerooms[socket.room]);
                            }
                        });
                    } else {
                        io.sockets.emit('error', socket.id, "No account found <br> Have you signed up? <br> Is your details correct?"); // notify player of and error by sending socket
                    }
                })
            }
        });
    });

1 个答案:

答案 0 :(得分:1)

我认为......

socket.on('login', function (uname, pword, email) {
===>
socket.on('login', function (data) {
// extract uname, pword, email from data
// ex) var jdata = JSON.parse(data); 
// var uname = jdata.uname;
// var pword = jdata.pword;
// var email = jdata.email