You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

websocket.go 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (c) 2015 Niels Freier
  2. // Copyright (c) 2015 Edmund Huber
  3. // released under the MIT license
  4. package irc
  5. import (
  6. "net/http"
  7. "time"
  8. "github.com/gorilla/websocket"
  9. )
  10. var upgrader = websocket.Upgrader{
  11. ReadBufferSize: 2048,
  12. WriteBufferSize: 2048,
  13. // If a WS session contains sensitive information, and you choose to use
  14. // cookies for authentication (during the HTTP(S) upgrade request), then
  15. // you should check that Origin is a domain under your control. If it
  16. // isn't, then it is possible for users of your site, visiting a naughty
  17. // Origin, to have a WS opened using their credentials. See
  18. // http://www.christian-schneider.net/CrossSiteWebSocketHijacking.html#main.
  19. // We don't care about Origin because the (IRC) authentication is contained
  20. // in the WS stream -- the WS session is not privileged when it is opened.
  21. CheckOrigin: func(r *http.Request) bool { return true },
  22. }
  23. type WSContainer struct {
  24. *websocket.Conn
  25. }
  26. func (this WSContainer) Read(msg []byte) (int, error) {
  27. ty, bytes, err := this.ReadMessage()
  28. if ty == websocket.TextMessage {
  29. n := copy(msg, []byte(string(bytes)+"\r\n\r\n"))
  30. return n, err
  31. }
  32. // Binary, and other kinds of messages, are thrown away.
  33. return 0, nil
  34. }
  35. func (this WSContainer) Write(msg []byte) (int, error) {
  36. err := this.WriteMessage(websocket.TextMessage, msg)
  37. return len(msg), err
  38. }
  39. func (this WSContainer) SetDeadline(t time.Time) error {
  40. if err := this.SetWriteDeadline(t); err != nil {
  41. return err
  42. }
  43. return this.SetReadDeadline(t)
  44. }