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

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