3.5 KiB
3.5 KiB
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
589fc832f9fc0f352b528e78 | Announce New Users | 2 | 宣布新用户 |
Description
将事件名称更改为“user”,并且数据传递一个对象,包含字段'name','currentUsers'和boolean'connected'(如果是连接则为true,对于断开发送的用户,则为false)。确保对我们有'用户计数'事件的两个点进行更改,并将断开连接设置为对'field'字段发送false,而不是像在connect上发出的事件那样为true。
io.emit('user', {name: socket.request.user.name, currentUsers, connected: true});
现在,您的客户端将拥有所有必要信息,以便在用户连接或断开连接时正确显示当前用户数和通知!要在客户端处理此事件,我们应该监听“用户”,然后使用jQuery将#num-users
的文本更改为“{NUMBER}在线用户”,并附加<li>
,以更新当前用户数<li>
使用ID为'messages'的无序列表,其中'{NAME}已{加/左}聊天。'。此实现可能如下所示: socket.on('user',function(data){ $('#num-users')。text(data.currentUsers +'users online'); var message = data.name; if(data.connected){ 消息+ ='已加入聊天。'; } else { 消息+ ='离开了聊天。'; } $('#messages')。append($('<li>')。html('<b>'+ message +'<\ / b>')); });当您认为自己已经做对时,请提交您的页面。
Instructions
Tests
tests:
- text: 使用name,currentUsers和connected发出事件“user”
testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/server.js") .then(data => { assert.match(data, /io.emit.*("|")user("|").*name.*currentUsers.*connected/gi, "You should have an event emitted named user sending name, currentUsers, and connected"); }, xhr => { throw new Error(xhr.statusText); })'
- text: 客户正确处理和显示事件'用户'中的新数据
testString: 'getUserInput => $.get(getUserInput("url")+ "/public/client.js") .then(data => { assert.match(data, /socket.on.*("|")user("|")[^]*num-users/gi, "You should change the text of #num-users within on your client within the "user" even listener to show the current users connected"); assert.match(data, /socket.on.*("|")user("|")[^]*messages.*li/gi, "You should append a list item to #messages on your client within the "user" event listener to annouce a user came or went"); }, xhr => { throw new Error(xhr.statusText); })'
Challenge Seed
Solution
// solution required