parent
9b043feca7
commit
8f0403e67c
@ -1,13 +1,13 @@
|
|||||||
enable: "etcd"
|
enable: "etcd"
|
||||||
etcd:
|
etcd:
|
||||||
rootDirectory: openim
|
rootDirectory: openim
|
||||||
address: [ localhost:12379 ]
|
address: [ 172.16.8.48:12379 ]
|
||||||
username: ''
|
username: ''
|
||||||
password: ''
|
password: ''
|
||||||
|
|
||||||
zookeeper:
|
zookeeper:
|
||||||
schema: openim
|
schema: openim
|
||||||
address: [ localhost:12181 ]
|
address: [ 172.16.8.48:12181 ]
|
||||||
username: ''
|
username: ''
|
||||||
password: ''
|
password: ''
|
||||||
|
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
package msggateway
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
sorted set
|
||||||
|
|
||||||
|
userID: 10000
|
||||||
|
|
||||||
|
USER_ONLINE:10000
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
platformID: 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
key score
|
||||||
|
|
||||||
|
E1 123456789
|
||||||
|
O1 234567895
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
@ -0,0 +1,58 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type userOnline struct {
|
||||||
|
rdb redis.UniversalClient
|
||||||
|
expire time.Duration
|
||||||
|
channelName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *userOnline) getUserOnlineKey(userID string) string {
|
||||||
|
return "USER_ONLINE:" + userID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *userOnline) SetUserOnline(ctx context.Context, userID string, online, offline []int32) error {
|
||||||
|
script := `
|
||||||
|
local key = KEYS[1]
|
||||||
|
local score = ARGV[3]
|
||||||
|
local num1 = redis.call("ZCARD", key)
|
||||||
|
redis.call("ZREMRANGEBYSCORE", key, "-inf", ARGV[2])
|
||||||
|
for i = 5, tonumber(ARGV[4])+4 do
|
||||||
|
redis.call("ZREM", key, ARGV[i])
|
||||||
|
end
|
||||||
|
local num2 = redis.call("ZCARD", key)
|
||||||
|
for i = 5+tonumber(ARGV[4]), #ARGV do
|
||||||
|
redis.call("ZADD", key, score, ARGV[i])
|
||||||
|
end
|
||||||
|
redis.call("EXPIRE", key, ARGV[1])
|
||||||
|
local num3 = redis.call("ZCARD", key)
|
||||||
|
local change = (num1 ~= num2) or (num2 ~= num3)
|
||||||
|
if change then
|
||||||
|
local members = redis.call("ZRANGE", key, 0, -1)
|
||||||
|
table.insert(members, KEYS[2])
|
||||||
|
redis.call("PUBLISH", KEYS[3], table.concat(members, ":"))
|
||||||
|
return 1
|
||||||
|
else
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
`
|
||||||
|
now := time.Now()
|
||||||
|
argv := make([]any, 0, 2+len(online)+len(offline))
|
||||||
|
argv = append(argv, int32(s.expire/time.Second), now.Unix(), now.Add(s.expire).Unix(), int32(len(offline)))
|
||||||
|
for _, platformID := range offline {
|
||||||
|
argv = append(argv, platformID)
|
||||||
|
}
|
||||||
|
for _, platformID := range online {
|
||||||
|
argv = append(argv, platformID)
|
||||||
|
}
|
||||||
|
keys := []string{s.getUserOnlineKey(userID), userID, s.channelName}
|
||||||
|
if err := s.rdb.Eval(ctx, script, keys, argv).Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"log"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newTestOnline() *userOnline {
|
||||||
|
opt := &redis.Options{
|
||||||
|
Addr: "172.16.8.48:16379",
|
||||||
|
Password: "openIM123",
|
||||||
|
DB: 1,
|
||||||
|
}
|
||||||
|
rdb := redis.NewClient(opt)
|
||||||
|
if err := rdb.Ping(context.Background()).Err(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return &userOnline{rdb: rdb, expire: time.Hour, channelName: "user_online"}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOnline(t *testing.T) {
|
||||||
|
ts := newTestOnline()
|
||||||
|
|
||||||
|
//err := ts.SetUserOnline(context.Background(), "1000", []int32{1, 2, 3}, []int32{4, 5, 6})
|
||||||
|
err := ts.SetUserOnline(context.Background(), "1000", nil, []int32{1, 2, 3})
|
||||||
|
|
||||||
|
t.Log(err)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
local function tableToString(tbl, separator)
|
||||||
|
local result = {}
|
||||||
|
for _, v in ipairs(tbl) do
|
||||||
|
table.insert(result, tostring(v))
|
||||||
|
end
|
||||||
|
return table.concat(result, separator)
|
||||||
|
end
|
||||||
|
|
||||||
|
local myTable = {"one", "two", "three"}
|
||||||
|
local result = tableToString(myTable, ":")
|
||||||
|
|
||||||
|
print(result)
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
func TestRecvOnline(t *testing.T) {
|
||||||
|
ts := newTestOnline()
|
||||||
|
ctx := context.Background()
|
||||||
|
pubsub := ts.rdb.Subscribe(ctx, "user_online")
|
||||||
|
|
||||||
|
// 等待订阅确认
|
||||||
|
_, err := pubsub.Receive(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not subscribe: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建一个通道来接收消息
|
||||||
|
ch := pubsub.Channel()
|
||||||
|
|
||||||
|
// 处理接收到的消息
|
||||||
|
for msg := range ch {
|
||||||
|
fmt.Printf("Received message from channel %s: %s\n", msg.Channel, msg.Payload)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue