ws.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package common
  2. import (
  3. "fmt"
  4. "net/http"
  5. api "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api"
  6. // errs "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/errs"
  7. "github.com/gorilla/websocket"
  8. context "github.com/kataras/iris/v12/context"
  9. )
  10. var (
  11. HubInstance = NewHub()
  12. upgrader = websocket.Upgrader{} // use default options
  13. )
  14. func init() {
  15. upgrader.CheckOrigin = func(r *http.Request) bool { return true }
  16. //Inicializa a thread do servidor de chat
  17. go HubInstance.Run()
  18. }
  19. // Converte a requisição em um ws.
  20. /*
  21. {
  22. "id": "ws",
  23. "entity": "Thread",
  24. "type": "",
  25. "path": "/ws",
  26. "template": "implement",
  27. "beforePersistAction": false,
  28. "httpMethod": "GET",
  29. "description": "Converte a requisição em um ws.",
  30. "response": "Thread",
  31. "request": "",
  32. "scopes": [],
  33. "middlewares": [],
  34. "postresponse": null,
  35. "parameterOrder": [],
  36. "parameters": [],
  37. "parametersmap": {}
  38. }
  39. */
  40. func WsUpgrader(ctx context.Context) {
  41. var (
  42. con, err = upgrader.Upgrade(ctx.ResponseWriter(), ctx.Request(), nil)
  43. )
  44. if err != nil {
  45. fmt.Println("error:", err)
  46. return
  47. }
  48. client := &WsClient{
  49. Id: api.Q(ctx, "i", ""),
  50. Hub: HubInstance,
  51. Conn: con,
  52. Send: make(chan *Event, 256),
  53. }
  54. HubInstance.Register <- client
  55. client.Handle()
  56. return
  57. }