My task is writing chat with history. So for creating history i need each of messages send to Mongodb and when I have a next connecting I need to getting all messages and, with loop, send to all clients that are connected to chat
This is code of my ChatServer
func ChatServer(ws *websocket.Conn) {
// Connecting to MongoDB, collection History
session, err := mgo.Dial("mongodb://******:*******@ds045795.mongolab.com:45795/catalog")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("catalog").C("History")
// fmt.Println(c.Find())
// Adding clients to the map
clientId := ws.RemoteAddr().String()
defer ws.Close()
clients[ws] = true
// Loop for receiving msg
for {
var msg string
// If can not read msg - delete client from map
if err := websocket.Message.Receive(ws, &msg); err != nil {
delete(clients, ws)
return
}
sendAll(msg)
err = c.Insert(&Connect{clientId, msg})
if err != nil {
log.Fatal(err)
}
}
}
So my problem is getting all the elements in order from the collection. I have no idea how to do it, because can not find the proper function in documentations. Maybe you have some other offers?