Browse Source

Implemented sending pings while in queue flush loop and added ping/pong logging.

Timestamps in log are now with microseconds.
pull/3/head
Simon Eisenmann 12 years ago
parent
commit
505e9b0703
  1. 26
      src/app/spreed-speakfreely-server/connection.go
  2. 5
      src/app/spreed-speakfreely-server/main.go

26
src/app/spreed-speakfreely-server/connection.go

@ -143,6 +143,7 @@ func (c *Connection) readPump() { @@ -143,6 +143,7 @@ func (c *Connection) readPump() {
c.ws.SetReadLimit(maxMessageSize)
c.ws.SetReadDeadline(time.Now().Add(pongWait))
c.ws.SetPongHandler(func(string) error {
log.Println("Received pong", c.Idx)
c.ws.SetReadDeadline(time.Now().Add(pongWait))
return nil
})
@ -234,18 +235,28 @@ func (c *Connection) writePump() { @@ -234,18 +235,28 @@ func (c *Connection) writePump() {
for len(c.queue) > 0 {
message := c.queue[0]
c.queue = c.queue[1:]
c.mutex.Unlock()
if ping {
// Send ping.
ping = false
c.mutex.Unlock()
if err := c.ping(); err != nil {
log.Println("Error while sending ping", c.Idx, err)
return
}
} else {
c.mutex.Unlock()
}
if err := c.write(websocket.TextMessage, message); err != nil {
log.Println("Error while writing", c.Idx, err)
return
}
c.mutex.Lock()
}
// Send pings.
if ping {
// Send ping.
ping = false
c.mutex.Unlock()
if err := c.write(websocket.PingMessage, []byte{}); err != nil {
if err := c.ping(); err != nil {
log.Println("Error while sending ping", c.Idx, err)
return
}
@ -257,6 +268,15 @@ func (c *Connection) writePump() { @@ -257,6 +268,15 @@ func (c *Connection) writePump() {
}
}
// Write ping message
func (c *Connection) ping() error {
log.Println("Sending ping", c.Idx)
defer func() {
log.Println("Sent ping", c.Idx)
}()
return c.write(websocket.PingMessage, []byte{})
}
// Write writes a message with the given opCode and payload.
func (c *Connection) write(opCode int, payload []byte) error {
c.ws.SetWriteDeadline(time.Now().Add(writeWait))

5
src/app/spreed-speakfreely-server/main.go

@ -116,6 +116,9 @@ func handleRoomView(room string, w http.ResponseWriter, r *http.Request) { @@ -116,6 +116,9 @@ func handleRoomView(room string, w http.ResponseWriter, r *http.Request) {
}
func runner(runtime phoenix.Runtime) error {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
rootFolder, err := runtime.GetString("http", "root")
if err != nil {
cwd, err2 := os.Getwd()
@ -324,7 +327,7 @@ func boot() error { @@ -324,7 +327,7 @@ func boot() error {
return nil
}
return phoenix.NewServer("mediastream-connector", "").
return phoenix.NewServer("server", "").
Config(configPath).
Log(logPath).
CpuProfile(cpuprofile).

Loading…
Cancel
Save