Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small compilation fix for tip #12

Open
wants to merge 3 commits into
base: testing
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package socketio

import (
"websocket"
"io"
"bytes"
"os"
"errors"
"io"
"strconv"
"websocket"
)

// Client is a toy interface.
type Client interface {
io.Closer

Dial(string, string) os.Error
Send(interface{}) os.Error
Dial(string, string) error
Send(interface{}) error
OnDisconnect(func())
OnMessage(func(Message))
SessionID() SessionID
Expand All @@ -38,7 +38,7 @@ func NewWebsocketClient(codec Codec) (wc *WebsocketClient) {
return
}

func (wc *WebsocketClient) Dial(rawurl string, origin string) (err os.Error) {
func (wc *WebsocketClient) Dial(rawurl string, origin string) (err error) {
var messages []Message
var nr int

Expand All @@ -54,18 +54,18 @@ func (wc *WebsocketClient) Dial(rawurl string, origin string) (err os.Error) {
buf := make([]byte, 2048)
if nr, err = wc.ws.Read(buf); err != nil {
wc.ws.Close()
return os.NewError("Dial: " + err.String())
return errors.New("Dial: " + err.Error())
}
wc.decBuf.Write(buf[0:nr])

if messages, err = wc.dec.Decode(); err != nil {
wc.ws.Close()
return os.NewError("Dial: " + err.String())
return errors.New("Dial: " + err.Error())
}

if len(messages) != 1 {
wc.ws.Close()
return os.NewError("Dial: expected exactly 1 message, but got " + strconv.Itoa(len(messages)))
return errors.New("Dial: expected exactly 1 message, but got " + strconv.Itoa(len(messages)))
}

// TODO: Fix me: The original Socket.IO codec does not have a special encoding for handshake
Expand All @@ -75,14 +75,14 @@ func (wc *WebsocketClient) Dial(rawurl string, origin string) (err os.Error) {
if _, ok := wc.codec.(SIOCodec); !ok {
if messages[0].Type() != MessageHandshake {
wc.ws.Close()
return os.NewError("Dial: expected handshake, but got " + messages[0].Data())
return errors.New("Dial: expected handshake, but got " + messages[0].Data())
}
}

wc.sessionid = SessionID(messages[0].Data())
if wc.sessionid == "" {
wc.ws.Close()
return os.NewError("Dial: received empty sessionid")
return errors.New("Dial: received empty sessionid")
}

wc.connected = true
Expand All @@ -96,7 +96,7 @@ func (wc *WebsocketClient) SessionID() SessionID {
}

func (wc *WebsocketClient) reader() {
var err os.Error
var err error
var nr int
var messages []Message
buf := make([]byte, 2048)
Expand Down Expand Up @@ -133,15 +133,15 @@ func (wc *WebsocketClient) OnMessage(f func(Message)) {
wc.onMessage = f
}

func (wc *WebsocketClient) Send(payload interface{}) os.Error {
func (wc *WebsocketClient) Send(payload interface{}) error {
if wc.ws == nil {
return ErrNotConnected
}

return wc.enc.Encode(wc.ws, payload)
}

func (wc *WebsocketClient) Close() os.Error {
func (wc *WebsocketClient) Close() error {
if !wc.connected {
return ErrNotConnected
}
Expand Down
10 changes: 5 additions & 5 deletions codec.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package socketio

import (
"io"
"os"
"bytes"
"errors"
"io"
)

var (
ErrMalformedPayload = os.NewError("malformed payload")
ErrMalformedPayload = errors.New("malformed payload")
)

// A Codec wraps Encode and Decode methods.
Expand All @@ -21,10 +21,10 @@ type Codec interface {
}

type Decoder interface {
Decode() ([]Message, os.Error)
Decode() ([]Message, error)
Reset()
}

type Encoder interface {
Encode(io.Writer, interface{}) os.Error
Encode(io.Writer, interface{}) error
}
18 changes: 9 additions & 9 deletions codec_sio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package socketio

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"json"
"os"
"strconv"
"utf8"
"unicode/utf8"
)

// The various delimiters used for framing in the socket.io protocol.
Expand Down Expand Up @@ -118,7 +118,7 @@ func (sc SIOCodec) NewEncoder() Encoder {
// of the following: a heartbeat, a handshake, []byte, string, int or anything
// than can be marshalled by the default json package. If payload can't be
// encoded or the writing fails, an error will be returned.
func (enc *sioEncoder) Encode(dst io.Writer, payload interface{}) (err os.Error) {
func (enc *sioEncoder) Encode(dst io.Writer, payload interface{}) (err error) {
enc.elem.Reset()

switch t := payload.(type) {
Expand Down Expand Up @@ -203,9 +203,9 @@ func (dec *sioDecoder) Reset() {
dec.length = 0
}

func (dec *sioDecoder) Decode() (messages []Message, err os.Error) {
func (dec *sioDecoder) Decode() (messages []Message, err error) {
messages = make([]Message, 0, 1)
var c int
var c rune

L:
for {
Expand All @@ -226,7 +226,7 @@ L:
if dec.buf.Len() == len(sioFrameDelim) {
if !bytes.Equal(dec.buf.Bytes(), sioFrameDelim) {
dec.Reset()
return nil, os.NewError("Malformed header")
return nil, errors.New("Malformed header")
}

dec.state = sioDecodeStateLength
Expand Down Expand Up @@ -257,7 +257,7 @@ L:

if !bytes.Equal(dec.buf.Bytes(), sioFrameDelim) {
dec.Reset()
return nil, os.NewError("Malformed header")
return nil, errors.New("Malformed header")
}

dec.state = sioDecodeStateData
Expand Down Expand Up @@ -308,7 +308,7 @@ L:
dec.buf.WriteRune(c)
}

if err == os.EOF {
if err == io.EOF {
err = nil
}

Expand Down
12 changes: 5 additions & 7 deletions codec_sio_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package socketio

import (
"testing"
"utf8"
"fmt"
"bytes"
"os"
"fmt"
"testing"
"unicode/utf8"
)

func frame(data string, json bool) string {
Expand Down Expand Up @@ -68,7 +67,6 @@ var encodeTests = []encodeTest{
},
}


type decodeTestMessage struct {
messageType uint8
data string
Expand Down Expand Up @@ -139,7 +137,7 @@ func TestDecode(t *testing.T) {
buf := new(bytes.Buffer)
dec := codec.NewDecoder(buf)
var messages []Message
var err os.Error
var err error

for _, test := range decodeTests {
t.Logf("in=%s out=%v", test.in, test.out)
Expand Down Expand Up @@ -176,7 +174,7 @@ func TestDecode(t *testing.T) {

func TestDecodeStreaming(t *testing.T) {
var messages []Message
var err os.Error
var err error
codec := SIOCodec{}
buf := new(bytes.Buffer)
dec := codec.NewDecoder(buf)
Expand Down
22 changes: 11 additions & 11 deletions codec_siostreaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package socketio

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"json"
"os"
"strconv"
"utf8"
"unicode/utf8"
)

// SIOStreamingCodec is the codec used by the official Socket.IO client by LearnBoost
Expand All @@ -26,7 +26,7 @@ func (sc SIOStreamingCodec) NewEncoder() Encoder {
// of the following: a heartbeat, a handshake, []byte, string, int or anything
// than can be marshalled by the default json package. If payload can't be
// encoded or the writing fails, an error will be returned.
func (enc *sioStreamingEncoder) Encode(dst io.Writer, payload interface{}) (err os.Error) {
func (enc *sioStreamingEncoder) Encode(dst io.Writer, payload interface{}) (err error) {
enc.elem.Reset()

switch t := payload.(type) {
Expand Down Expand Up @@ -115,10 +115,10 @@ func (dec *sioStreamingDecoder) Reset() {
dec.length = 0
}

func (dec *sioStreamingDecoder) Decode() (messages []Message, err os.Error) {
func (dec *sioStreamingDecoder) Decode() (messages []Message, err error) {
messages = make([]Message, 0, 1)
var c int
var typ uint
var c rune
var typ uint64

L:
for {
Expand All @@ -136,7 +136,7 @@ L:
switch dec.state {
case sioStreamingDecodeStateType:
if c == ':' {
if typ, err = strconv.Atoui(dec.buf.String()); err != nil {
if typ, err = strconv.ParseUint(dec.buf.String(), 10, 0); err != nil {
dec.Reset()
return nil, err
}
Expand Down Expand Up @@ -186,7 +186,7 @@ L:
case '\n':
if dec.buf.Len() == 0 {
dec.Reset()
return nil, os.NewError("expecting key, but got...")
return nil, errors.New("expecting key, but got...")
}
dec.key = dec.buf.String()
if dec.msg.annotations == nil {
Expand Down Expand Up @@ -251,14 +251,14 @@ L:
continue
} else {
dec.Reset()
return nil, os.NewError("Expecting trailer but got... " + string(c))
return nil, errors.New("Expecting trailer but got... " + string(c))
}
}

dec.buf.WriteRune(c)
}

if err == os.EOF {
if err == io.EOF {
err = nil
}

Expand Down
12 changes: 5 additions & 7 deletions codec_siostreaming_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package socketio

import (
"testing"
"utf8"
"fmt"
"bytes"
"fmt"
"testing"
"unicode/utf8"
"unsafe"
"os"
)

func streamingFrame(data string, typ int, json bool) string {
Expand Down Expand Up @@ -77,7 +76,6 @@ var streamingEncodeTests = []streamingEncodeTest{
},
}


type streamingDecodeTestMessage struct {
messageType uint8
data string
Expand Down Expand Up @@ -148,7 +146,7 @@ func TestStreamingDecode(t *testing.T) {
buf := new(bytes.Buffer)
dec := codec.NewDecoder(buf)
var messages []Message
var err os.Error
var err error

for _, test := range streamingDecodeTests {
t.Logf("in=%s out=%v", test.in, test.out)
Expand Down Expand Up @@ -185,7 +183,7 @@ func TestStreamingDecode(t *testing.T) {

func TestStreamingDecodeStreaming(t *testing.T) {
var messages []Message
var err os.Error
var err error
codec := SIOStreamingCodec{}
buf := new(bytes.Buffer)
dec := codec.NewDecoder(buf)
Expand Down
13 changes: 8 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package socketio

import "log"
import (
"log"
"time"
)

// Config represents a set of configurable settings used by the server
type Config struct {
Expand All @@ -16,11 +19,11 @@ type Config struct {
ReadBufferSize int

// The interval between heartbeats
HeartbeatInterval int64
HeartbeatInterval time.Duration

// Period in ns during which the client must reconnect or it is considered
// disconnected.
ReconnectTimeout int64
ReconnectTimeout time.Duration

// Origins to allow for cross-domain requests.
// For example: ["localhost:8080", "myblog.com:*"].
Expand All @@ -43,8 +46,8 @@ var DefaultConfig = Config{
MaxConnections: 0,
QueueLength: 10,
ReadBufferSize: 2048,
HeartbeatInterval: 10e9,
ReconnectTimeout: 10e9,
HeartbeatInterval: time.Duration(10 * time.Second),
ReconnectTimeout: time.Duration(10 * time.Second),
Origins: nil,
Transports: DefaultTransports,
Codec: SIOCodec{},
Expand Down
Loading