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.4KB

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