Advertisement
Xlonix

hub.go

Dec 2nd, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.10 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "net/http"
  6.     "time"
  7.     "net/url"
  8.     "github.com/gorilla/websocket"
  9.     "strings"
  10.     "strconv"
  11. )
  12.  
  13. var (
  14.     //Pass connections between threads
  15.     conchan chan *websocket.Conn
  16.     //Master quit channel (not implemented)
  17.     quit chan int16
  18.     //Base HTTP server for websocket upgrade
  19.     serv = &http.Server{Addr: "localhost:3000", ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second}
  20.     //Upgrader
  21.     wsupgr = websocket.Upgrader{CheckOrigin: checkReq}
  22.     //Shows how many users are
  23.     // [0] - in the lobby
  24.     // [1] - being matched
  25.     // [2] - in game
  26.     states = []uint64{0, 0, 0}
  27. )
  28.  
  29. //Makes shure the origin of the request is not compromised
  30. func checkReq(r *http.Request) bool{
  31.     u, err := url.Parse(r.Header["Origin"][0])
  32.     if err != nil {
  33.         return false
  34.     }
  35.     return strings.HasPrefix(r.Host, u.Host)
  36. }
  37.  
  38. //Accepts a http request, translates it to a websocket, and passes it into the rest of the code
  39. func hndl(w http.ResponseWriter, r *http.Request) {
  40.     c, err := wsupgr.Upgrade(w, r, nil)
  41.     if err != nil {
  42.         fmt.Println("[ERR]", err)
  43.         return
  44.     }
  45.     lobby(c)
  46. }
  47.  
  48. //Returns a string representation of [states]
  49. func getUsers() string {
  50.     return fmt.Sprintf("%s:%s:%s", strconv.FormatUint(states[0]), strconv.FormatUint(states[1]), strconv.FormatUint(states[2]))
  51. }
  52.  
  53. //Accepts a websocket connection and performs basic tasks
  54. func lobby(conn *websocket.Conn){
  55.     states[0]++
  56.     fmt.Println("In Lobby")
  57.     conn.WriteJSON(DataJSON{"lobby", ""})
  58.     var dat DataJSON
  59.     for {
  60.         err := conn.ReadJSON(&dat)
  61.         if err != nil {
  62.             states[0]--
  63.             fmt.Println("[ALERT]", err)
  64.             conn.Close()
  65.             return
  66.         }
  67.         switch dat.Data{
  68.             case "match":
  69.                 fmt.Println("Matching")
  70.                 conn.WriteJSON(DataJSON{"matching", ""})
  71.                 conchan <- conn
  72.                 return
  73.             case "state":
  74.                 fmt.Println("State")
  75.                 conn.WriteJSON(DataJSON{"matching", getUsers()})
  76.             default:
  77.                 break
  78.         }
  79.     }
  80. }
  81.  
  82. //Starting point for program
  83. func main() {
  84.     conchan = make(chan *websocket.Conn)
  85.     quit = make(chan int16)
  86.     go grouper(conchan, quit)
  87.     http.HandleFunc("/lobby", hndl)
  88.     fmt.Println("Error: ", serv.ListenAndServe())
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement