Example #1
0
func Listen(s *Session) {
	for s.Websocket != nil {
		var message WebsocketMessage
		err := websocket.JSON.Receive(s.Websocket, &message)

		if err != nil {
			p, _ := save.GetPlayer(s.Username)
			location := p.Entity.Component(save.COMP_Location).(*save.Location)
			u, _ := save.GetUniverse(location.UniverseId)

			p.Entity.RemoveComponent(s)
			u.Remove(p.Entity)

			s.Websocket.Close()
			return
		}

		go ReceiveMessage(s, &message)
	}
}
Example #2
0
var WebsocketHandler = websocket.Handler(func(conn *websocket.Conn) {
	s := readsession(conn.Request())

	if s == nil {
		logrus.Warn("server.WebsocketHandler: Failure: Session missing")
		return
	}

	s.Lock()
	defer s.Unlock()

	s.Websocket = conn

	p, err := save.GetPlayer(s.Username)

	if err != nil {
		logrus.WithFields(logrus.Fields{
			"Username": s.Username,
		}).Error("server.Websocket: " + err.Error())
		return
	}

	p.Entity.AddComponent(s)

	location := p.Entity.Component(save.COMP_Location).(*save.Location)
	u, _ := save.GetUniverse(location.UniverseId)
	u.Add(p.Entity)

	Listen(s)
})