mirror of https://github.com/tidwall/redcon.git
Added pub/sub documentation
This commit is contained in:
parent
7c780e8bd7
commit
4dcf45bac0
42
README.md
42
README.md
|
@ -18,6 +18,7 @@ Features
|
||||||
- Support for pipelining and telnet commands
|
- Support for pipelining and telnet commands
|
||||||
- Works with Redis clients such as [redigo](https://github.com/garyburd/redigo), [redis-py](https://github.com/andymccurdy/redis-py), [node_redis](https://github.com/NodeRedis/node_redis), and [jedis](https://github.com/xetorthio/jedis)
|
- Works with Redis clients such as [redigo](https://github.com/garyburd/redigo), [redis-py](https://github.com/andymccurdy/redis-py), [node_redis](https://github.com/NodeRedis/node_redis), and [jedis](https://github.com/xetorthio/jedis)
|
||||||
- [TLS Support](#tls-example)
|
- [TLS Support](#tls-example)
|
||||||
|
- Compatible [pub/sub](#pub_sub)
|
||||||
- Multithreaded
|
- Multithreaded
|
||||||
|
|
||||||
Installing
|
Installing
|
||||||
|
@ -35,6 +36,8 @@ Here's a full example of a Redis clone that accepts:
|
||||||
- SET key value
|
- SET key value
|
||||||
- GET key
|
- GET key
|
||||||
- DEL key
|
- DEL key
|
||||||
|
- PUBLISH channel message
|
||||||
|
- (P)SUBSCRIBE channel
|
||||||
- PING
|
- PING
|
||||||
- QUIT
|
- QUIT
|
||||||
|
|
||||||
|
@ -60,12 +63,47 @@ var addr = ":6380"
|
||||||
func main() {
|
func main() {
|
||||||
var mu sync.RWMutex
|
var mu sync.RWMutex
|
||||||
var items = make(map[string][]byte)
|
var items = make(map[string][]byte)
|
||||||
|
var ps redcon.PubSub
|
||||||
go log.Printf("started server at %s", addr)
|
go log.Printf("started server at %s", addr)
|
||||||
err := redcon.ListenAndServe(addr,
|
err := redcon.ListenAndServe(addr,
|
||||||
func(conn redcon.Conn, cmd redcon.Command) {
|
func(conn redcon.Conn, cmd redcon.Command) {
|
||||||
switch strings.ToLower(string(cmd.Args[0])) {
|
switch strings.ToLower(string(cmd.Args[0])) {
|
||||||
default:
|
default:
|
||||||
conn.WriteError("ERR unknown command '" + string(cmd.Args[0]) + "'")
|
conn.WriteError("ERR unknown command '" + string(cmd.Args[0]) + "'")
|
||||||
|
case "publish":
|
||||||
|
// Publish to all pub/sub subscribers and return the number of
|
||||||
|
// messages that were sent.
|
||||||
|
if len(cmd.Args) != 3 {
|
||||||
|
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
count := ps.Publish(string(cmd.Args[1]), string(cmd.Args[2]))
|
||||||
|
conn.WriteInt(count)
|
||||||
|
case "subscribe", "psubscribe":
|
||||||
|
// Subscribe to a pub/sub channel. The `Psubscribe` and
|
||||||
|
// `Subscribe` operations will detach the connection from the
|
||||||
|
// event handler and manage all network I/O for this connection
|
||||||
|
// in the background.
|
||||||
|
if len(cmd.Args) < 2 {
|
||||||
|
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
command := strings.ToLower(string(cmd.Args[0]))
|
||||||
|
for i := 1; i < len(cmd.Args); i++ {
|
||||||
|
if command == "psubscribe" {
|
||||||
|
ps.Psubscribe(conn, string(cmd.Args[i]))
|
||||||
|
} else {
|
||||||
|
ps.Subscribe(conn, string(cmd.Args[i]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "detach":
|
||||||
|
hconn := conn.Detach()
|
||||||
|
log.Printf("connection has been detached")
|
||||||
|
go func() {
|
||||||
|
defer hconn.Close()
|
||||||
|
hconn.WriteString("OK")
|
||||||
|
hconn.Flush()
|
||||||
|
}()
|
||||||
case "ping":
|
case "ping":
|
||||||
conn.WriteString("PONG")
|
conn.WriteString("PONG")
|
||||||
case "quit":
|
case "quit":
|
||||||
|
@ -110,12 +148,12 @@ func main() {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
func(conn redcon.Conn) bool {
|
func(conn redcon.Conn) bool {
|
||||||
// use this function to accept or deny the connection.
|
// Use this function to accept or deny the connection.
|
||||||
// log.Printf("accept: %s", conn.RemoteAddr())
|
// log.Printf("accept: %s", conn.RemoteAddr())
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
func(conn redcon.Conn, err error) {
|
func(conn redcon.Conn, err error) {
|
||||||
// this is called when the connection has been closed
|
// This is called when the connection has been closed
|
||||||
// log.Printf("closed: %s, err: %v", conn.RemoteAddr(), err)
|
// log.Printf("closed: %s, err: %v", conn.RemoteAddr(), err)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
|
@ -21,6 +21,8 @@ func main() {
|
||||||
default:
|
default:
|
||||||
conn.WriteError("ERR unknown command '" + string(cmd.Args[0]) + "'")
|
conn.WriteError("ERR unknown command '" + string(cmd.Args[0]) + "'")
|
||||||
case "publish":
|
case "publish":
|
||||||
|
// Publish to all pub/sub subscribers and return the number of
|
||||||
|
// messages that were sent.
|
||||||
if len(cmd.Args) != 3 {
|
if len(cmd.Args) != 3 {
|
||||||
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
|
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
|
||||||
return
|
return
|
||||||
|
@ -28,6 +30,10 @@ func main() {
|
||||||
count := ps.Publish(string(cmd.Args[1]), string(cmd.Args[2]))
|
count := ps.Publish(string(cmd.Args[1]), string(cmd.Args[2]))
|
||||||
conn.WriteInt(count)
|
conn.WriteInt(count)
|
||||||
case "subscribe", "psubscribe":
|
case "subscribe", "psubscribe":
|
||||||
|
// Subscribe to a pub/sub channel. The `Psubscribe` and
|
||||||
|
// `Subscribe` operations will detach the connection from the
|
||||||
|
// event handler and manage all network I/O for this connection
|
||||||
|
// in the background.
|
||||||
if len(cmd.Args) < 2 {
|
if len(cmd.Args) < 2 {
|
||||||
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
|
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
|
||||||
return
|
return
|
||||||
|
@ -92,12 +98,12 @@ func main() {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
func(conn redcon.Conn) bool {
|
func(conn redcon.Conn) bool {
|
||||||
// use this function to accept or deny the connection.
|
// Use this function to accept or deny the connection.
|
||||||
// log.Printf("accept: %s", conn.RemoteAddr())
|
// log.Printf("accept: %s", conn.RemoteAddr())
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
func(conn redcon.Conn, err error) {
|
func(conn redcon.Conn, err error) {
|
||||||
// this is called when the connection has been closed
|
// This is called when the connection has been closed
|
||||||
// log.Printf("closed: %s, err: %v", conn.RemoteAddr(), err)
|
// log.Printf("closed: %s, err: %v", conn.RemoteAddr(), err)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue