Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: graarh/golang-socketio
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: bigbluebutton-bot/golang-socketio
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 7 commits
  • 11 files changed
  • 1 contributor

Commits on Feb 13, 2023

  1. 🚚 move example

    JulianKropp committed Feb 13, 2023
    Copy the full SHA
    956dede View commit details
  2. ➕ Add go.mod

    JulianKropp committed Feb 13, 2023
    Copy the full SHA
    5a34b8d View commit details

Commits on Feb 14, 2023

  1. Add cookie support

    JulianKropp committed Feb 14, 2023
    Copy the full SHA
    0a283a8 View commit details
  2. Copy the full SHA
    7b83ae8 View commit details

Commits on Feb 22, 2023

  1. Copy the full SHA
    9cd2944 View commit details

Commits on Aug 16, 2023

  1. 🎨 Rename module

    JulianKropp committed Aug 16, 2023
    Copy the full SHA
    bbc29a9 View commit details

Commits on Nov 14, 2023

  1. Copy the full SHA
    0b9dca8 View commit details
Showing with 141 additions and 70 deletions.
  1. +1 −1 README.md
  2. +2 −2 {examples → _examples}/client.go
  3. +2 −2 {examples → _examples}/server.go
  4. +23 −15 client.go
  5. +5 −0 go.mod
  6. +2 −0 go.sum
  7. +15 −8 handler.go
  8. +20 −11 loop.go
  9. +8 −4 send.go
  10. +46 −24 server.go
  11. +17 −3 transport/websocket.go
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ Examples directory contains simple client and server.

### Installation

go get github.com/graarh/golang-socketio
go get github.com/bigbluebutton-bot/golang-socketio

### Simple server usage

4 changes: 2 additions & 2 deletions examples/client.go → _examples/client.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

import (
"github.com/graarh/golang-socketio"
"github.com/graarh/golang-socketio/transport"
"log"
"runtime"
"time"

"github.com/bigbluebutton-bot/golang-socketio/transport"
)

type Channel struct {
4 changes: 2 additions & 2 deletions examples/server.go → _examples/server.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

import (
"github.com/graarh/golang-socketio"
"github.com/graarh/golang-socketio/transport"
"log"
"net/http"
"time"

"github.com/bigbluebutton-bot/golang-socketio/transport"
)

type Channel struct {
38 changes: 23 additions & 15 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package gosocketio

import (
"github.com/graarh/golang-socketio/transport"
"strconv"

"github.com/bigbluebutton-bot/golang-socketio/transport"
)

const (
webSocketProtocol = "ws://"
webSocketProtocol = "ws://"
webSocketSecureProtocol = "wss://"
socketioUrl = "/socket.io/?EIO=3&transport=websocket"
socketioUrl = "/socket.io/?EIO=3&transport=websocket"
)

/**
/*
*
Socket.io client representation
*/
type Client struct {
methods
Channel
}

/**
/*
*
Get ws/wss url by host and port
*/
*/
func GetUrl(host string, port int, secure bool) string {
var prefix string
if secure {
@@ -32,33 +35,38 @@ func GetUrl(host string, port int, secure bool) string {
return prefix + host + ":" + strconv.Itoa(port) + socketioUrl
}

/**
func NewClient() *Client {
c := &Client{}
c.initChannel()
c.initMethods()
return c
}

/*
*
connect to host and initialise socket.io protocol
The correct ws protocol url example:
ws://myserver.com/socket.io/?EIO=3&transport=websocket
You can use GetUrlByHost for generating correct url
*/
func Dial(url string, tr transport.Transport) (*Client, error) {
c := &Client{}
c.initChannel()
c.initMethods()

func (c *Client) Dial(url string, tr transport.Transport) error {
var err error
c.conn, err = tr.Connect(url)
if err != nil {
return nil, err
return err
}

go inLoop(&c.Channel, &c.methods)
go outLoop(&c.Channel, &c.methods)
go pinger(&c.Channel)

return c, nil
return nil
}

/**
/*
*
Close client connection
*/
func (c *Client) Close() {
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/bigbluebutton-bot/golang-socketio

go 1.19

require github.com/gorilla/websocket v1.5.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
23 changes: 15 additions & 8 deletions handler.go
Original file line number Diff line number Diff line change
@@ -2,9 +2,10 @@ package gosocketio

import (
"encoding/json"
"github.com/graarh/golang-socketio/protocol"
"sync"
"reflect"
"sync"

"github.com/bigbluebutton-bot/golang-socketio/protocol"
)

const (
@@ -13,12 +14,14 @@ const (
OnError = "error"
)

/**
/*
*
System handler function for internal event processing
*/
type systemHandler func(c *Channel)

/**
/*
*
Contains maps of message processing functions
*/
type methods struct {
@@ -29,14 +32,16 @@ type methods struct {
onDisconnection systemHandler
}

/**
/*
*
create messageHandlers map
*/
func (m *methods) initMethods() {
m.messageHandlers = make(map[string]*caller)
}

/**
/*
*
Add message processing function, and bind it to given method
*/
func (m *methods) On(method string, f interface{}) error {
@@ -52,7 +57,8 @@ func (m *methods) On(method string, f interface{}) error {
return nil
}

/**
/*
*
Find message processing function associated with given method
*/
func (m *methods) findMethod(method string) (*caller, bool) {
@@ -79,7 +85,8 @@ func (m *methods) callLoopEvent(c *Channel, event string) {
f.callFunc(c, &struct{}{})
}

/**
/*
*
Check incoming message
On ack_resp - look for waiter
On ack_req - look for processing function and send ack_resp
31 changes: 20 additions & 11 deletions loop.go
Original file line number Diff line number Diff line change
@@ -3,11 +3,12 @@ package gosocketio
import (
"encoding/json"
"errors"
"github.com/graarh/golang-socketio/protocol"
"github.com/graarh/golang-socketio/transport"
"net/http"
"sync"
"time"

"github.com/bigbluebutton-bot/golang-socketio/protocol"
"github.com/bigbluebutton-bot/golang-socketio/transport"
)

const (
@@ -18,7 +19,8 @@ var (
ErrorWrongHeader = errors.New("Wrong header")
)

/**
/*
*
engine.io header to send or receive
*/
type Header struct {
@@ -28,7 +30,8 @@ type Header struct {
PingTimeout int `json:"pingTimeout"`
}

/**
/*
*
socket.io connection handler
use IsAlive to check that handler is still working
@@ -53,7 +56,8 @@ type Channel struct {
requestHeader http.Header
}

/**
/*
*
create channel, map, and set active
*/
func (c *Channel) initChannel() {
@@ -63,14 +67,16 @@ func (c *Channel) initChannel() {
c.alive = true
}

/**
/*
*
Get id of current socket connection
*/
func (c *Channel) Id() string {
return c.header.Sid
}

/**
/*
*
Checks that Channel is still alive
*/
func (c *Channel) IsAlive() bool {
@@ -80,7 +86,8 @@ func (c *Channel) IsAlive() bool {
return c.alive
}

/**
/*
*
Close channel
*/
func closeChannel(c *Channel, m *methods, args ...interface{}) error {
@@ -110,7 +117,7 @@ func closeChannel(c *Channel, m *methods, args ...interface{}) error {
return nil
}

//incoming messages loop, puts incoming messages to In channel
// incoming messages loop, puts incoming messages to In channel
func inLoop(c *Channel, m *methods) error {
for {
pkg, err := c.conn.GetMessage()
@@ -149,7 +156,8 @@ func AmountOfOverflooded() int64 {
return int64(len(overflooded))
}

/**
/*
*
outgoing messages loop, sends messages from channel to socket
*/
func outLoop(c *Channel, m *methods) error {
@@ -180,7 +188,8 @@ func outLoop(c *Channel, m *methods) error {
return nil
}

/**
/*
*
Pinger sends ping messages for keeping connection alive
*/
func pinger(c *Channel) {
12 changes: 8 additions & 4 deletions send.go
Original file line number Diff line number Diff line change
@@ -3,17 +3,19 @@ package gosocketio
import (
"encoding/json"
"errors"
"github.com/graarh/golang-socketio/protocol"
"log"
"time"

"github.com/bigbluebutton-bot/golang-socketio/protocol"
)

var (
ErrorSendTimeout = errors.New("Timeout")
ErrorSocketOverflood = errors.New("Socket overflood")
)

/**
/*
*
Send message packet to socket
*/
func send(msg *protocol.Message, c *Channel, args interface{}) error {
@@ -47,7 +49,8 @@ func send(msg *protocol.Message, c *Channel, args interface{}) error {
return nil
}

/**
/*
*
Create packet based on given data and send it
*/
func (c *Channel) Emit(method string, args interface{}) error {
@@ -59,7 +62,8 @@ func (c *Channel) Emit(method string, args interface{}) error {
return send(msg, c, args)
}

/**
/*
*
Create ack packet based on given data and send it and receive response
*/
func (c *Channel) Ack(method string, args interface{}, timeout time.Duration) (string, error) {
Loading