zishang520 / socket.io-go-redis

Not fully ready yet, there may be many problems
MIT License
2 stars 4 forks source link

socket.io-go-redis

Build Status GoDoc

How to use

package main

import (
    "context"
    "fmt"
    "os"
    "os/signal"
    "syscall"

    "github.com/redis/go-redis/v9"
    "github.com/zishang520/engine.io/v2/types"
    "github.com/zishang520/socket.io-go-redis/adapter"
    r_types "github.com/zishang520/socket.io-go-redis/types"
    "github.com/zishang520/socket.io/v2/socket"
    // "github.com/zishang520/socket.io-go-redis/emitter"
)

func main() {

    redisClient := r_types.NewRedisClient(context.TODO(), redis.NewClient(&redis.Options{
        Addr:     "127.0.0.1:6379",
        Username: "",
        Password: "",
        DB:       0,
    }))

    redisClient.On("error", func(a ...any) {
        fmt.Println(a)
    })

    config := socket.DefaultServerOptions()
    config.SetAdapter(&adapter.RedisAdapterBuilder{
        Redis: redisClient,
        Opts:  &adapter.RedisAdapterOptions{},
    })
    httpServer := types.CreateServer(nil)
    io := socket.NewServer(httpServer, config)
    io.On("connection", func(clients ...any) {
        client := clients[0].(*socket.Socket)
        client.On("event", func(datas ...any) {
        })
        client.On("disconnect", func(...any) {
        })
    })
    httpServer.Listen("127.0.0.1:9000", nil)

    // emitter.NewEmitter(redisClient, nil, "/web") // more ....

    exit := make(chan struct{})
    SignalC := make(chan os.Signal)

    signal.Notify(SignalC, os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
    go func() {
        for s := range SignalC {
            switch s {
            case os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
                close(exit)
                return
            }
        }
    }()

    <-exit
    httpServer.Close(nil)
    os.Exit(0)
}

Tests

Standalone tests can be run with make test which will run the golang tests.

You can run the tests locally using the following command:

make test

Support

issues

Development

To contribute patches, run tests or benchmarks, make sure to clone the repository:

git clone git://github.com/zishang520/socket.io-go-redis.git

Then:

cd socket.io-go-redis
make test

See the Tests section above for how to run tests before submitting any patches.

License

MIT