admin管理员组文章数量:1293642
Learning Node.js, Express.js and Socket.io.
Made a Chat an it works so far.
Now I would like to emit to the Client, that a user has entered or left the chat by emiting a variable that indicates that...
Ist that possible?
So something like this:
Server.js
var users = [];
var inout;
function updateUsers(){
io.emit('users', users, 'inout', inout);
}
Client:
var socket = io.connect( 'http://'+window.location.hostname+':3000' );
socket.on('users', function(data){
// how to get the 'inout' here? }
Thanks for any Help... ;)
Learning Node.js, Express.js and Socket.io.
Made a Chat an it works so far.
Now I would like to emit to the Client, that a user has entered or left the chat by emiting a variable that indicates that...
Ist that possible?
So something like this:
Server.js
var users = [];
var inout;
function updateUsers(){
io.emit('users', users, 'inout', inout);
}
Client:
var socket = io.connect( 'http://'+window.location.hostname+':3000' );
socket.on('users', function(data){
// how to get the 'inout' here? }
Thanks for any Help... ;)
Share Improve this question edited Mar 13, 2020 at 15:13 JVE999 3,51712 gold badges61 silver badges95 bronze badges asked Nov 1, 2016 at 0:02 bernd pfefferbernd pfeffer 331 gold badge1 silver badge3 bronze badges 2- 1 You ;) don't ;) need ;) all ;) those ;) winky ;) faces ;). (ps, this might be of help.) – Darren Commented Nov 1, 2016 at 0:18
- Oh no! Where did all my smilies go? ;O I like to show my emotions.. hehe ;) – bernd pfeffer Commented Nov 1, 2016 at 7:52
2 Answers
Reset to default 4The simplest way is to emit object:
let users = []
let inout
function updateUsers() {
io.emit('users', {users, inout});
}
I think you need to read the documentation more clearly, because this doesn't look like Socket.io.
Here's an example of sending an array, along with some other data:
var io = require("socket.io")(3001);
io.emit("myEvent", {
somethingToSendToBrowser: "Hello",
arrayToSendToBrowser: [
"I'm the data that will get sent.",
"I'm some more data.",
"Here's the third piece of data."
]
});
<script src="/node_modules/socket.io-client/socket.io.js"></script>
<script>
var socket = io("http://localhost:3001");
socket.on("myEvent", function(data){
console.log(data.arrayToSendToBrowser);
// ["I'm the data that will get sent.", "I'm some more data.", "Here's the third piece of data.]"
});
</script>
本文标签: javascriptsocketio How to emit an Array and a Variable simultaneouslyStack Overflow
版权声明:本文标题:javascript - socket.io: How to emit an Array and a Variable simultaneously - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741585449a2386820.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论