| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package stomp
- import (
- "encoding/json"
- "log"
- "battlecamp-go/flag"
- "github.com/go-stomp/stomp"
- )
- var stompConnection *stomp.Conn
- func getStompConnection() *stomp.Conn {
- if stompConnection == nil {
- var err error
- stompConnection, err = stomp.Dial("tcp", *flag.StompUrl)
- if(err != nil) {
- log.Printf("Error creating stomp connection error: %v\n", err)
- } else {
- log.Printf("Created stomp connection to %v", *flag.StompUrl)
- }
- }
- return stompConnection
- }
- func SendJsonPlayerJoin(topic string, v interface{}) {
- sendJson(topic, "vnd.battlecamp.player", v)
- }
- func SendJsonPlayerUpdate(topic string, v interface{}) {
- sendJson(topic, "vnd.battlecamp.update", v)
- }
- func SendJson(topic string, v interface{}) {
- sendJson(topic, "application/json;charset=utf-8", v)
- }
- func sendJson(topic, contentType string, v interface{}) {
- b, _ := json.Marshal(v)
- getStompConnection().Send("/topic/battlecamp-go."+topic, contentType, b, stomp.SendOpt.NoContentLength)
- }
- func Subscribe(topic string) chan *stomp.Message {
- result, _ := getStompConnection().Subscribe("/topic/battlecamp-go."+topic, stomp.AckAuto)
- return result.C
- }
|