mirror of
https://github.com/jeremyd/ergo.git
synced 2026-07-29 02:08:12 -07:00
WebSocket layer to be able to connect "web" client
Currently working with a mini irc js implem, the flow: * PASS * NICK * USER * JOIN * PRIVMSG works and the ping/pong timeout keep the communication open.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package irc
|
||||
|
||||
import (
|
||||
"github.com/gorilla/websocket"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
}
|
||||
|
||||
type WSContainer struct {
|
||||
conn *websocket.Conn
|
||||
}
|
||||
|
||||
func (this WSContainer) Close() error {
|
||||
return this.conn.Close()
|
||||
}
|
||||
|
||||
func (this WSContainer) LocalAddr() net.Addr {
|
||||
return this.conn.LocalAddr()
|
||||
}
|
||||
|
||||
func (this WSContainer) RemoteAddr() net.Addr {
|
||||
return this.conn.RemoteAddr()
|
||||
}
|
||||
|
||||
func (this WSContainer) Read(msg []byte) (int, error) {
|
||||
_, tmp, err := this.conn.ReadMessage()
|
||||
str := (string)(tmp)
|
||||
n := copy(msg, ([]byte)(str+CRLF+CRLF))
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (this WSContainer) Write(msg []byte) (int, error) {
|
||||
err := this.conn.WriteMessage(1, msg)
|
||||
return len(msg), err
|
||||
}
|
||||
|
||||
func (this WSContainer) SetDeadline(t time.Time) error {
|
||||
err := this.conn.SetWriteDeadline(t)
|
||||
err = this.conn.SetReadDeadline(t)
|
||||
return err
|
||||
}
|
||||
|
||||
func (this WSContainer) SetReadDeadline(t time.Time) error {
|
||||
return this.conn.SetReadDeadline(t)
|
||||
}
|
||||
|
||||
func (this WSContainer) SetWriteDeadline(t time.Time) error {
|
||||
return this.conn.SetWriteDeadline(t)
|
||||
}
|
||||
Reference in New Issue
Block a user