-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a34ea5e
commit c23b355
Showing
9 changed files
with
745 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package socketio | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
) | ||
|
||
var ( | ||
ErrorInvalidConnection = errors.New("invalid connection") | ||
ErrorUUIDDuplication = errors.New("UUID already exists") | ||
) | ||
|
||
type connections struct { | ||
sync.RWMutex | ||
conn map[string]*Socket | ||
} | ||
|
||
func (l *connections) set(socket *Socket) { | ||
l.Lock() | ||
l.conn[socket.Id] = socket | ||
l.Unlock() | ||
} | ||
|
||
func (l *connections) get(key string) (*Socket, error) { | ||
l.RLock() | ||
ret, ok := l.conn[key] | ||
l.RUnlock() | ||
if !ok { | ||
return nil, ErrorInvalidConnection | ||
} | ||
return ret, nil | ||
} | ||
|
||
func (l *connections) all() []*Socket { | ||
l.RLock() | ||
ret := make([]*Socket, 0) | ||
for _, socket := range l.conn { | ||
ret = append(ret, socket) | ||
} | ||
l.RUnlock() | ||
return ret | ||
} | ||
|
||
func (l *connections) delete(key string) { | ||
l.Lock() | ||
delete(l.conn, key) | ||
l.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package socketio | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type connectionEventCallback func(payload *Socket) | ||
|
||
type connectionEvent struct { | ||
sync.RWMutex | ||
list map[string][]connectionEventCallback | ||
} | ||
|
||
func (l *connectionEvent) set(event string, callback connectionEventCallback) { | ||
l.Lock() | ||
l.list[event] = append(l.list[event], callback) | ||
l.Unlock() | ||
} | ||
|
||
func (l *connectionEvent) get(event string) []connectionEventCallback { | ||
l.RLock() | ||
defer l.RUnlock() | ||
if _, ok := l.list[event]; !ok { | ||
return make([]connectionEventCallback, 0) | ||
} | ||
ret := make([]connectionEventCallback, 0) | ||
ret = append(ret, l.list[event]...) | ||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package engineio | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"strconv" | ||
) | ||
|
||
type PacketType int | ||
|
||
const ( | ||
OPEN PacketType = iota | ||
CLOSE | ||
PING | ||
PONG | ||
MESSAGE | ||
UPGRADE | ||
NOOP | ||
) | ||
|
||
func (id PacketType) String() string { | ||
return strconv.Itoa(int(id)) | ||
} | ||
|
||
type writer struct { | ||
t PacketType | ||
i int64 | ||
w io.Writer | ||
} | ||
|
||
func (w *writer) Write(p []byte) (int, error) { | ||
paserData := append([]byte(w.t.String()), p...) | ||
n, err := w.w.Write(paserData) | ||
w.i += int64(n) | ||
return n, err | ||
} | ||
|
||
func WriteTo(w io.Writer, t PacketType, arg ...interface{}) (int64, error) { | ||
writer := writer{ | ||
t: t, | ||
w: w, | ||
} | ||
if len(arg) > 0 { | ||
err := json.NewEncoder(&writer).Encode(arg[0]) | ||
return writer.i, err | ||
} else { | ||
_, err := writer.Write([]byte{}) | ||
return writer.i, err | ||
} | ||
} | ||
|
||
func WriteByte(w io.Writer, t PacketType, p []byte) (int, error) { | ||
writer := writer{ | ||
t: t, | ||
w: w, | ||
} | ||
return writer.Write(p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package engineio | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type ConnParameters struct { | ||
PingInterval time.Duration | ||
PingTimeout time.Duration | ||
SID string | ||
Upgrades []string | ||
MaxPayload int | ||
} | ||
|
||
type jsonParameters struct { | ||
SID string `json:"sid"` | ||
Upgrades []string `json:"upgrades,omitempty"` | ||
PingInterval int `json:"pingInterval,omitempty"` | ||
PingTimeout int `json:"pingTimeout,omitempty"` | ||
MaxPayload int `json:"maxPayload,omitempty"` | ||
} | ||
|
||
// func ReadConnParameters(r io.Reader) (ConnParameters, error) { | ||
// var param jsonParameters | ||
// if err := json.NewDecoder(r).Decode(¶m); err != nil { | ||
// return ConnParameters{}, err | ||
// } | ||
|
||
// return ConnParameters{ | ||
// SID: param.SID, | ||
// Upgrades: param.Upgrades, | ||
// PingInterval: time.Duration(param.PingInterval) * time.Millisecond, | ||
// PingTimeout: time.Duration(param.PingTimeout) * time.Millisecond, | ||
// MaxPayload: param.MaxPayload, | ||
// }, nil | ||
// } | ||
|
||
func (p ConnParameters) ToJson() jsonParameters { | ||
arg := jsonParameters{ | ||
SID: p.SID, | ||
Upgrades: p.Upgrades, | ||
PingInterval: int(p.PingInterval / time.Millisecond), | ||
PingTimeout: int(p.PingTimeout / time.Millisecond), | ||
MaxPayload: int(p.MaxPayload), | ||
} | ||
return arg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package socketio | ||
|
||
import "sync" | ||
|
||
type EventPayload struct { | ||
Name string //event name | ||
SID string //socket id | ||
Socket *Socket | ||
Error error | ||
Data []interface{} | ||
} | ||
|
||
type eventCallback func(payload *EventPayload) | ||
|
||
type listeners struct { | ||
sync.RWMutex | ||
list map[string][]eventCallback | ||
} | ||
|
||
func (l *listeners) set(event string, callback eventCallback) { | ||
l.Lock() | ||
l.list[event] = append(l.list[event], callback) | ||
l.Unlock() | ||
} | ||
|
||
func (l *listeners) get(event string) []eventCallback { | ||
l.RLock() | ||
defer l.RUnlock() | ||
if _, ok := l.list[event]; !ok { | ||
return make([]eventCallback, 0) | ||
} | ||
ret := make([]eventCallback, 0) | ||
ret = append(ret, l.list[event]...) | ||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package socketio | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type Namespace struct { | ||
name string | ||
sockets connections | ||
onConnection connectionEvent | ||
} | ||
|
||
func newNamespace(name string) *Namespace { | ||
return &Namespace{ | ||
name: name, | ||
sockets: connections{ | ||
conn: make(map[string]*Socket), | ||
}, | ||
onConnection: connectionEvent{ | ||
list: make(map[string][]connectionEventCallback), | ||
}, | ||
} | ||
} | ||
|
||
func (nps *Namespace) OnConnection(fn connectionEventCallback) { | ||
nps.onConnection.set("connection", fn) | ||
} | ||
|
||
func (nps *Namespace) Emit(event string, agrs ...interface{}) error { | ||
for _, socket := range nps.sockets.all() { | ||
socket.Emit(event, agrs...) | ||
} | ||
return nil | ||
} | ||
|
||
type namespaces struct { | ||
sync.RWMutex | ||
list map[string]*Namespace | ||
} | ||
|
||
func (n *namespaces) create(name string) *Namespace { | ||
n.Lock() | ||
n.list[name] = newNamespace(name) | ||
ret := n.list[name] | ||
n.Unlock() | ||
return ret | ||
} | ||
|
||
func (n *namespaces) all() []*Namespace { | ||
n.RLock() | ||
ret := make([]*Namespace, 0) | ||
for _, nps := range n.list { | ||
ret = append(ret, nps) | ||
} | ||
n.RUnlock() | ||
return ret | ||
} | ||
|
||
func (n *namespaces) get(name string) *Namespace { | ||
n.RLock() | ||
defer n.RUnlock() | ||
ret, ok := n.list[name] | ||
if !ok { | ||
return nil | ||
} | ||
return ret | ||
} |
Oops, something went wrong.