Ask coding questions

← Back to all posts
CHAT room adding commands
h
RossJames (271)

Hi, what would I have to do to this code to add commands and be able to kick people/ delete messages? This would need to be only if there name was something special like a password. At the moment, I have added my own name by using 54r3 as the name. Any ideas?

Answered by Geocube101 (588) [earned 5 cycles]
View Answer
Commentshotnewtop
Geocube101 (588)

The owner could send an event to the server, which will broadcast an event to all clients with data such as the target username. Any client with the matching username will send an event to the server (to get the clients socket). The server can then call the socket.disconnect() to kick the clients socket.

I normally create a kick page and redirect the client there, otherwise, they will remain in the room, but won't be able to do anything

RossJames (271)

@Geocube101 Ok, Thanks. Will have a look

RossJames (271)

@Geocube101 I am not sure how to target it at a user i have now set it up so if you type leave, then you get disconnected, but still cant aim it at a specific user

Geocube101 (588)

@RossJames Sorry for long response time. The way I did it, was getting a string input (the client to kick's username), and emitting it to the server. The server then would emit to all clients the username. If any client had that username, it would emit back to the server. Upon receiving the event, the socket which sent the event is kicked.

You could either setup an input field or a prompt to get the target client's username.
You could also have a button which receives the username (from one of the methods listed above) and fires a function

//The owner
//The following function can be linked to a button
function kick_user(username)
{
  socket.emit('kick_user', username)
}
//Server Side - Inside the 'connection' event handler
socket.on('kick_user', function(username)
{
  io.sockets.emit('kick', username);
});
socket.on('kick_confirmed', function()
{
  socket.disconnect();
});
//Client Side
//'currentUsername' is the name that the client has selected for themselves
socket.on('kick', function(username)
{
  if (username == currentUsername)
  {
    socket.emit('kick_confirmed');
  }
});
Wubium_Dubium (0)

This is not your own repl.it. It is stolen from @Jser, and it can get you a strike on here, or maybe a kick or ban. I do not know how it works, but I believe there will be consequences if this is plagarism.

themaka (181)

Hi there @Wubium_Dubium and others.

Let's ease up on accusing people of theft.

It's not 'stolen' -- repls are public and may be forked by anyone. Presenting it as your own work is certainly unethical, but it is very common to fork a repl (or clone a GitHub repo, etc) as a starting place to work from and nothing is wrong with that.

@RossJames asked about adding more commands to the chat - I don't see any claims he was the original author, so I'm not sure why we're accusing him of theft.

That said, it would be nice if you credited the original creator in a comment, @RossJames

JSer (615)

Oh crap, who forked my repl?

PYer (3254)

This seems pretty similar to something i saw from @JSer