{this.renderUser(this.state.currentUser) }
{this.state.users.length} users online:
{ this.state.users.map(u => this.renderUser(u)) }
{ this.state.connectedToUserId ? : null}
);
}
renderUser(u) {
return (
this.connectToUser(u.userId) } style={{
cursor: "pointer", padding: "4px",
background: u.userId === this.state.connectedToUserId ? "#ffc" : ""
}}>
{u.displayName} {u.userId === this.state.currentUser.userId ? " (me)" : ""}
);
}
connectToUser(userId) {
if (userId === this.state.currentUser.userId) return;
this.requestUsersState(userId);
var connectedChannels = this.state.channels.filter(x => x !== "home");
$.ss.updateSubscriber({
SubscribeChannels: userChannel(userId),
UnsubscribeChannels: connectedChannels.join(',')
}, r => {
this.setState({
channels: r.channels,
connectedToUserId: userId
});
});
}
disconnect() {
$.ss.unsubscribeFromChannels([userChannel(this.state.connectedToUserId)]);
this.setState({ connectedToUserId: null });
}
requestUsersState(userId) {
return $.ss.postJSON(`/send-user/${userId}?selector=cmd.getState`,
{ replyTo: this.state.currentUser.userId });
}
refreshUsers() {
$.getJSON("/event-subscribers?channels=home", users => {
this.setState({ users: filterUsers(users, this.state.currentUser.userId) });
});
}
}
const userChannel = (userId) => "u_" + userId;
const filterUsers = (users, userId) =>
users.filter(x => x.userId !== userId).sort((x, y) => x.userId.localeCompare(y.userId));