R
R
Roman Rakzin2016-04-08 22:18:52
go
Roman Rakzin, 2016-04-08 22:18:52

What is the best way to store an array of users online in golang?

What is the best way to store an array of users online?
Users connect via websocket.
There are slices with the following structures
Users {Id,Name .....}
SocketsOnline{Id, *webSocketType, (UserId)}
UsersOnline{UserId,map [SocketId]}
Rooms{Id,NameRoom}
SocketsInRoom{RoomId,SocketId} // or UserId instead of a socket
when a user connects, a socket is created and added to the slice from the SocketsOnline type.
If I need to send a message to everyone, I go through the entire slice and send the message to everyone.
But I want to store pointers to sockets for each user of type UsersOnline in parallel.
That is, when socket 1 is connected - I know that this is user id333 - I create an object in a slice of type UsersOnline and want to throw a link to socket 1.
If socket 2 is connected, user id333 - I add more.
If the user is disconnected, I remove the socket from SocketsOnline and everything is fine.
Poidee- if you create pointers to the sockets of the user, then when removed from SocketsOnline, it should disappear in UsersOnline.
But if the socket is disconnected, then UserOnline itself will not be deleted. (How to clean it up?) The
question is, how to implement it? Or perhaps a different logic is needed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Yakovenko, 2016-04-14
@Xazzzi

When disconnecting a socket, go through to check for the presence of other sockets from the matching UserId, if there are none, remove the user from UsersOnline. Technically - defer the check function/method after successful authorization of the user, when a link to it is entered into the UsersOnline slice

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question