admin管理员组文章数量:1415664
I followed the net tuts tutorial to build a simple chat application with socket and node, and now I'm trying to extend the app to allow people to play a game I've written, so I want to let people list the games available, but I can't seem to pass even a simple test array from server to client.
I'll let the code speak for itself:
Relevant Server Code:
var games = ["test"];
io.sockets.on('connection', function (socket) {
socket.emit('message', { message: 'wele to the chat' });
socket.on('gameList', function (data) {
io.sockets.emit("games",games);
});
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
});
ClientSide:
window.games = [];
$("#listGames").click(function(){
socket.emit("gameList", function(data){
window.games = data;
})
})
So I console.log games before and after the button click, and it's always just an empty array, when in my head, it should contain the string "test"
.
So where did I go wrong?
Note: Added jQuery to the codebase even though the tutorial missed it.
I followed the net tuts tutorial to build a simple chat application with socket and node, and now I'm trying to extend the app to allow people to play a game I've written, so I want to let people list the games available, but I can't seem to pass even a simple test array from server to client.
I'll let the code speak for itself:
Relevant Server Code:
var games = ["test"];
io.sockets.on('connection', function (socket) {
socket.emit('message', { message: 'wele to the chat' });
socket.on('gameList', function (data) {
io.sockets.emit("games",games);
});
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
});
ClientSide:
window.games = [];
$("#listGames").click(function(){
socket.emit("gameList", function(data){
window.games = data;
})
})
So I console.log games before and after the button click, and it's always just an empty array, when in my head, it should contain the string "test"
.
So where did I go wrong?
Note: Added jQuery to the codebase even though the tutorial missed it.
Share Improve this question edited Sep 16, 2013 at 16:03 hexacyanide 91.9k31 gold badges166 silver badges162 bronze badges asked Sep 16, 2013 at 15:59 Jason NicholsJason Nichols 3,7603 gold badges32 silver badges51 bronze badges1 Answer
Reset to default 4You are using socket.emit()
to attempt to receive data. That only sends data to the server. You need a games
event handler on your client to handle the event accordingly.
window.games = [];
$("#listGames").click(function() {
socket.emit('gameList');
});
socket.on('games', function (games) {
window.games = games;
});
The event handler for games
is what will fire when you execute io.sockets.emit('games', games);
on the server side.
Also make sure to always pass an object as the response over Socket.IO. Change the server-side code from : var games=[];
to var games={'games':[]};
or something similar.
本文标签:
版权声明:本文标题:javascript - Using Node and Socket.io I am trying to pass a simple array and I'm getting nothing. What did I miss? - Sta 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745199303a2647294.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论