main_20201129141444.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package main
  2. import (
  3. "errors"
  4. "flag"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. "strconv"
  9. //wss "git.alfi.li/gamelang/frontend/wsserver"
  10. )
  11. type ExampleConfig struct {
  12. Http struct {
  13. Listen string // ip:port
  14. }
  15. Kafka struct {
  16. Enabled bool
  17. UserManagerBroker string
  18. WorldManagerBroker string
  19. }
  20. }
  21. func DefaultConfig() ExampleConfig {
  22. config := ExampleConfig{}
  23. config.Http.Listen = "0.0.0.0:8080"
  24. config.Kafka.Enabled = false
  25. return config
  26. }
  27. type WebRTCPeer struct {
  28. id int
  29. ws *WSPeer
  30. }
  31. type exampleWSServer struct {
  32. wsserver WSServer
  33. peers map[int]*WebRTCPeer
  34. }
  35. func (ewss exampleWSServer) register(peer *WSPeer) *WebRTCPeer {
  36. id := 1
  37. if len(ewss.peers) > 0 {
  38. for i := 2; i <= len(ewss.peers); i++ {
  39. _, ok := ewss.peers[i]
  40. if !ok {
  41. id = i
  42. break
  43. }
  44. }
  45. if id == 1 {
  46. id = len(ewss.peers) + 1
  47. }
  48. }
  49. wrtcPeer, ok := ewss.peers[id]
  50. if ok {
  51. wrtcPeer.ws = peer
  52. } else {
  53. wrtcPeer = &WebRTCPeer{id: id, ws: peer}
  54. ewss.peers[id] = wrtcPeer
  55. }
  56. msg := WSMsg{ID: id, Mtype: "I", Payload: strconv.Itoa(id)}
  57. peer.Answer(msg)
  58. return wrtcPeer
  59. }
  60. func (ewss exampleWSServer) unregister(peer WebRTCPeer) {
  61. delete(ewss.peers, peer.id)
  62. }
  63. func (ewss exampleWSServer) getWebRTCPeerFromWSPeer(wspeer *WSPeer) *WebRTCPeer {
  64. for _, webRTCPeer := range ewss.peers {
  65. if wspeer.uuid == webRTCPeer.ws.uuid {
  66. return webRTCPeer
  67. }
  68. }
  69. return nil
  70. }
  71. func (ewss exampleWSServer) RenderHandler() []Handler {
  72. handler := []Handler{}
  73. handler = append(handler, Handler{MType: "echo", Callback: func(msg WSMsg, peer *WSPeer) error {
  74. log.Printf("echo from \"%s\" with \"%s\"", peer.uuid.String(), msg.Payload)
  75. remsg := WSMsg{Mtype: "echo", Payload: msg.Payload}
  76. peer.Answer(remsg)
  77. return nil
  78. }})
  79. handler = append(handler, Handler{MType: "J", Callback: func(msg WSMsg, peer *WSPeer) error {
  80. peer.Answer(WSMsg{Mtype: "J", Payload: "default"})
  81. log.Printf("join from \"%s\" with \"%s\"", peer.uuid.String(), msg.Payload)
  82. webRTCPeer := ewss.register(peer)
  83. for otherPeerID, _ := range ewss.peers {
  84. if otherPeerID == webRTCPeer.id {
  85. continue
  86. }
  87. peerMsg := WSMsg{Mtype: "N", Payload: strconv.Itoa(webRTCPeer.id)}
  88. go ewss.peers[otherPeerID].ws.Answer(peerMsg)
  89. }
  90. return nil
  91. }})
  92. handler = append(handler, Handler{MType: "O", Callback: func(msg WSMsg, peer *WSPeer) error {
  93. log.Printf("offer from \"%s\" for \"%d\"", peer.uuid.String(), msg.ID)
  94. otherPeer, ok := ewss.peers[msg.ID]
  95. if !ok {
  96. return errors.New(fmt.Sprintf("Peer with id \"%d\" not found", msg.ID))
  97. }
  98. otherPeer.ws.Answer(WSMsg{Mtype: "O", Payload: msg.Payload, ID: msg.ID})
  99. return nil
  100. }})
  101. handler = append(handler, Handler{MType: "A", Callback: func(msg WSMsg, peer *WSPeer) error {
  102. log.Printf("answer from \"%s\" with \"%s\"", peer.uuid.String(), msg.Payload)
  103. otherPeer := ewss.peers[msg.ID]
  104. otherPeer.ws.Answer(WSMsg{Mtype: "A", Payload: msg.Payload, ID: msg.ID})
  105. return nil
  106. }})
  107. handler = append(handler, Handler{MType: "C", Callback: func(msg WSMsg, peer *WSPeer) error {
  108. log.Printf("candidate from \"%s\" with \"%s\"", peer.uuid.String(), msg.Payload)
  109. otherPeer := ewss.peers[msg.ID]
  110. otherPeer.ws.Answer(WSMsg{Mtype: "C", Payload: msg.Payload, ID: msg.ID})
  111. return nil
  112. }})
  113. return handler
  114. }
  115. func main() {
  116. wsPtr := flag.String("ws", ":8080", "websocket listen port")
  117. wssPtr := flag.String("wss", ":8443", "wesocker secure listen port")
  118. certPtr := flag.String("cert", "", "wesocker secure server certificate")
  119. keyPtr := flag.String("key", "", "wesocker secure server key")
  120. flag.Parse()
  121. wsserver := NewWSServer()
  122. ewss := exampleWSServer{wsserver: *wsserver}
  123. ewss.peers = map[int]*WebRTCPeer{}
  124. ewss.wsserver.RegisterHandler(ewss.RenderHandler())
  125. http.HandleFunc("/play", ewss.wsserver.WebsocketHandler)
  126. log.Println("websocket listening on", *wsPtr)
  127. go http.ListenAndServe(*wsPtr, nil)
  128. log.Println("websocket listening on", *wssPtr)
  129. err := http.ListenAndServeTLS(*wssPtr, *certPtr, *keyPtr, nil)
  130. if err != nil {
  131. fmt.Println(err.Error())
  132. }
  133. }