在golang中我需要创建一个websocket连接并跟踪所有连接的用户,广播或用户到用户的通信等。 作为我计划使用该连接的内存地址的每个用户的标识符。 这种方法有什么缺点吗?在我看来,这不应该造成任何问题,因为每个套接字是在websocket连接结束时销毁的请求端的上下文中创建的,只要连接存活,程序就应该保留所有这些内存地址。
var Socket *socket //i did this in order Socket to be ready without init out of the box just like Mutex //check init function to see how i handle this
type socket struct {
connectedUsers []*User
}
type User struct {
Id unsafe.Pointer
Conn *websocket.Conn
}
func init() {
var sock = new(socket)
sock.connectedUsers = make([]*User, 0)
Socket = sock
}
//NewConnection add a new *User into connected users list
func (*socket) NewConnection(sock *websocket.Conn) {
usr:=new(User)
usr.Conn=sock
usr.Id=unsafe.Pointer(usr)
Socket.connectedUsers=append(Socket.connectedUsers,usr)
}
//then i can do something like
//theOtherUser:=*(*websocket.Conn)(usr.Id)//this will give the connection of the user (hopefully)