Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Commit

Permalink
Add DB option to RedisAdapterOptions (#564)
Browse files Browse the repository at this point in the history
* Add option DB to RedisAdapterOptions
  • Loading branch information
ezerozen authored Feb 8, 2023
1 parent 1a06b55 commit 0349c35
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion _examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ _, err := server.Adapter(&socketio.RedisAdapterOptions{
Addr: "127.0.0.1:6379",
Host: "127.0.0.1",
Port: "6379",
Prefix: "socket.io",
Prefix: "socket.io",
DB: 1,
})
if err != nil {
log.Fatal("error:", err)
Expand Down
2 changes: 2 additions & 0 deletions adapter_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type RedisAdapterOptions struct {
Prefix string
Network string
Password string
// DB : specifies the database to select when dialing a connection.
DB int
}

func (ro *RedisAdapterOptions) getAddr() string {
Expand Down
3 changes: 3 additions & 0 deletions redis_broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func newRedisBroadcast(nsp string, opts *RedisAdapterOptions) (*redisBroadcast,
if len(opts.Password) > 0 {
redisOpts = append(redisOpts, redis.DialPassword(opts.Password))
}
if opts.DB > 0 {
redisOpts = append(redisOpts, redis.DialDatabase(opts.DB))
}

pub, err := redis.Dial(opts.Network, addr, redisOpts...)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func (s *Server) Adapter(opts *RedisAdapterOptions) (bool, error) {
if len(opts.Password) > 0 {
redisOpts = append(redisOpts, redis.DialPassword(opts.Password))
}
if opts.DB > 0 {
redisOpts = append(redisOpts, redis.DialDatabase(opts.DB))
}

conn, err := redis.Dial(opts.Network, opts.getAddr(), redisOpts...)
if err != nil {
Expand Down

0 comments on commit 0349c35

Please sign in to comment.