Browse Source

Golint fixes

master
Robbie Trencheny 8 years ago
parent
commit
b5a06028d4
  1. 8
      examples/gophers/main.go
  2. 14
      melody.go
  3. 5
      melody_test.go
  4. 14
      session.go

8
examples/gophers/main.go

@ -1,14 +1,16 @@
package main package main
import ( import (
"github.com/gin-gonic/gin"
"github.com/olahol/melody"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"github.com/gin-gonic/gin"
"github.com/olahol/melody"
) )
// GopherInfo contains information about the gopher on screen
type GopherInfo struct { type GopherInfo struct {
ID, X, Y string ID, X, Y string
} }
@ -35,7 +37,7 @@ func main() {
} }
gophers[s] = &GopherInfo{strconv.Itoa(counter), "0", "0"} gophers[s] = &GopherInfo{strconv.Itoa(counter), "0", "0"}
s.Write([]byte("iam " + gophers[s].ID)) s.Write([]byte("iam " + gophers[s].ID))
counter += 1
counter++
lock.Unlock() lock.Unlock()
}) })

14
melody.go

@ -163,7 +163,7 @@ func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) error {
// HandleRequestWithKeys does the same as HandleRequest but populates session.Keys with keys. // HandleRequestWithKeys does the same as HandleRequest but populates session.Keys with keys.
func (m *Melody) HandleRequestWithKeys(w http.ResponseWriter, r *http.Request, keys map[string]interface{}) error { func (m *Melody) HandleRequestWithKeys(w http.ResponseWriter, r *http.Request, keys map[string]interface{}) error {
if m.hub.closed() { if m.hub.closed() {
return errors.New("Melody instance is closed.")
return errors.New("melody instance is closed")
} }
conn, err := m.Upgrader.Upgrade(w, r, nil) conn, err := m.Upgrader.Upgrade(w, r, nil)
@ -204,7 +204,7 @@ func (m *Melody) HandleRequestWithKeys(w http.ResponseWriter, r *http.Request, k
// Broadcast broadcasts a text message to all sessions. // Broadcast broadcasts a text message to all sessions.
func (m *Melody) Broadcast(msg []byte) error { func (m *Melody) Broadcast(msg []byte) error {
if m.hub.closed() { if m.hub.closed() {
return errors.New("Melody instance is closed.")
return errors.New("melody instance is closed")
} }
message := &envelope{t: websocket.TextMessage, msg: msg} message := &envelope{t: websocket.TextMessage, msg: msg}
@ -216,7 +216,7 @@ func (m *Melody) Broadcast(msg []byte) error {
// BroadcastFilter broadcasts a text message to all sessions that fn returns true for. // BroadcastFilter broadcasts a text message to all sessions that fn returns true for.
func (m *Melody) BroadcastFilter(msg []byte, fn func(*Session) bool) error { func (m *Melody) BroadcastFilter(msg []byte, fn func(*Session) bool) error {
if m.hub.closed() { if m.hub.closed() {
return errors.New("Melody instance is closed.")
return errors.New("melody instance is closed")
} }
message := &envelope{t: websocket.TextMessage, msg: msg, filter: fn} message := &envelope{t: websocket.TextMessage, msg: msg, filter: fn}
@ -235,7 +235,7 @@ func (m *Melody) BroadcastOthers(msg []byte, s *Session) error {
// BroadcastBinary broadcasts a binary message to all sessions. // BroadcastBinary broadcasts a binary message to all sessions.
func (m *Melody) BroadcastBinary(msg []byte) error { func (m *Melody) BroadcastBinary(msg []byte) error {
if m.hub.closed() { if m.hub.closed() {
return errors.New("Melody instance is closed.")
return errors.New("melody instance is closed")
} }
message := &envelope{t: websocket.BinaryMessage, msg: msg} message := &envelope{t: websocket.BinaryMessage, msg: msg}
@ -247,7 +247,7 @@ func (m *Melody) BroadcastBinary(msg []byte) error {
// BroadcastBinaryFilter broadcasts a binary message to all sessions that fn returns true for. // BroadcastBinaryFilter broadcasts a binary message to all sessions that fn returns true for.
func (m *Melody) BroadcastBinaryFilter(msg []byte, fn func(*Session) bool) error { func (m *Melody) BroadcastBinaryFilter(msg []byte, fn func(*Session) bool) error {
if m.hub.closed() { if m.hub.closed() {
return errors.New("Melody instance is closed.")
return errors.New("melody instance is closed")
} }
message := &envelope{t: websocket.BinaryMessage, msg: msg, filter: fn} message := &envelope{t: websocket.BinaryMessage, msg: msg, filter: fn}
@ -266,7 +266,7 @@ func (m *Melody) BroadcastBinaryOthers(msg []byte, s *Session) error {
// Close closes the melody instance and all connected sessions. // Close closes the melody instance and all connected sessions.
func (m *Melody) Close() error { func (m *Melody) Close() error {
if m.hub.closed() { if m.hub.closed() {
return errors.New("Melody instance is already closed.")
return errors.New("melody instance is already closed")
} }
m.hub.exit <- &envelope{t: websocket.CloseMessage, msg: []byte{}} m.hub.exit <- &envelope{t: websocket.CloseMessage, msg: []byte{}}
@ -278,7 +278,7 @@ func (m *Melody) Close() error {
// Use the FormatCloseMessage function to format a proper close message payload. // Use the FormatCloseMessage function to format a proper close message payload.
func (m *Melody) CloseWithMsg(msg []byte) error { func (m *Melody) CloseWithMsg(msg []byte) error {
if m.hub.closed() { if m.hub.closed() {
return errors.New("Melody instance is already closed.")
return errors.New("melody instance is already closed")
} }
m.hub.exit <- &envelope{t: websocket.CloseMessage, msg: msg} m.hub.exit <- &envelope{t: websocket.CloseMessage, msg: msg}

5
melody_test.go

@ -2,7 +2,6 @@ package melody
import ( import (
"bytes" "bytes"
"github.com/gorilla/websocket"
"math/rand" "math/rand"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -11,6 +10,8 @@ import (
"testing" "testing"
"testing/quick" "testing/quick"
"time" "time"
"github.com/gorilla/websocket"
) )
type TestServer struct { type TestServer struct {
@ -145,7 +146,7 @@ func TestLen(t *testing.T) {
if rand.Float32() < disconnect { if rand.Float32() < disconnect {
conns[i] = nil conns[i] = nil
disconnected += 1
disconnected++
conn.Close() conn.Close()
continue continue
} }

14
session.go

@ -22,20 +22,20 @@ type Session struct {
func (s *Session) writeMessage(message *envelope) { func (s *Session) writeMessage(message *envelope) {
if s.closed() { if s.closed() {
s.melody.errorHandler(s, errors.New("Tried to write to closed a session."))
s.melody.errorHandler(s, errors.New("tried to write to closed a session"))
return return
} }
select { select {
case s.output <- message: case s.output <- message:
default: default:
s.melody.errorHandler(s, errors.New("Session message buffer is full."))
s.melody.errorHandler(s, errors.New("session message buffer is full"))
} }
} }
func (s *Session) writeRaw(message *envelope) error { func (s *Session) writeRaw(message *envelope) error {
if s.closed() { if s.closed() {
return errors.New("Trie to write to a closed session.")
return errors.New("trie to write to a closed session")
} }
s.conn.SetWriteDeadline(time.Now().Add(s.melody.Config.WriteWait)) s.conn.SetWriteDeadline(time.Now().Add(s.melody.Config.WriteWait))
@ -141,7 +141,7 @@ func (s *Session) readPump() {
// Write writes message to session. // Write writes message to session.
func (s *Session) Write(msg []byte) error { func (s *Session) Write(msg []byte) error {
if s.closed() { if s.closed() {
return errors.New("Session is closed.")
return errors.New("session is closed")
} }
s.writeMessage(&envelope{t: websocket.TextMessage, msg: msg}) s.writeMessage(&envelope{t: websocket.TextMessage, msg: msg})
@ -152,7 +152,7 @@ func (s *Session) Write(msg []byte) error {
// WriteBinary writes a binary message to session. // WriteBinary writes a binary message to session.
func (s *Session) WriteBinary(msg []byte) error { func (s *Session) WriteBinary(msg []byte) error {
if s.closed() { if s.closed() {
return errors.New("Session is closed.")
return errors.New("session is closed")
} }
s.writeMessage(&envelope{t: websocket.BinaryMessage, msg: msg}) s.writeMessage(&envelope{t: websocket.BinaryMessage, msg: msg})
@ -163,7 +163,7 @@ func (s *Session) WriteBinary(msg []byte) error {
// Close closes session. // Close closes session.
func (s *Session) Close() error { func (s *Session) Close() error {
if s.closed() { if s.closed() {
return errors.New("Session is already closed.")
return errors.New("session is already closed")
} }
s.writeMessage(&envelope{t: websocket.CloseMessage, msg: []byte{}}) s.writeMessage(&envelope{t: websocket.CloseMessage, msg: []byte{}})
@ -175,7 +175,7 @@ func (s *Session) Close() error {
// Use the FormatCloseMessage function to format a proper close message payload. // Use the FormatCloseMessage function to format a proper close message payload.
func (s *Session) CloseWithMsg(msg []byte) error { func (s *Session) CloseWithMsg(msg []byte) error {
if s.closed() { if s.closed() {
return errors.New("Session is already closed.")
return errors.New("session is already closed")
} }
s.writeMessage(&envelope{t: websocket.CloseMessage, msg: msg}) s.writeMessage(&envelope{t: websocket.CloseMessage, msg: msg})

Loading…
Cancel
Save