예제 #1
0
파일: main.go 프로젝트: koomee/TestGame
func GameAction(game gamedef.Game) rack.Middleware {
	ws := websocketer.New()
	ws.OnOpen(rack.Func(func(vars map[string]interface{}, next func()) {
		c, p := V(vars).Get()
		conn := websocketer.V(vars).GetSocket()

		if p != nil {
			p.(*player.Player).AddConnection(conn)
		} else {
			V(vars).Set(c, (*player.Spectator)(conn))
		}
	}))
	ws.OnClose(rack.Func(func(vars map[string]interface{}, next func()) {
		_, p := V(vars).Get()
		conn := websocketer.V(vars).GetSocket()

		p.RemoveConnection(conn)
	}))
	ws.OnMessage(rack.Func(func(vars map[string]interface{}, next func()) {
		c, p := V(vars).Get()
		m := (websocketer.V)(vars).GetMessage()

		c.PlayerAction(p, *m.(*string))
	}))
	return ws

}
func New() rack.Middleware {
	ws := websocketer.New()
	ws.OnOpen(wsOpener)
	ws.OnClose(wsCloser)
	ws.OnMessage(wsMessager)
	ws.UseJSON()
	ws.OnStorage(func() interface{} {
		var m WebsocketMessage
		return &m
	})

	return ws
}