github.com/glide-im/glide@v1.6.0/example/client/rpc_client_example.go (about) 1 package main 2 3 import ( 4 "github.com/glide-im/glide/im_service/client" 5 "github.com/glide-im/glide/pkg/gate" 6 "github.com/glide-im/glide/pkg/messages" 7 "github.com/glide-im/glide/pkg/rpc" 8 "github.com/glide-im/glide/pkg/subscription/subscription_impl" 9 ) 10 11 /// 如何控制消息服务器, 用户连接, 发布订阅接口 12 /// 用于 HTTP API 接口等非 IM 业务配合使用 13 14 func main() { 15 16 RpcGatewayClientUpdateClient() 17 return 18 19 // 消息网关接口(用户管理用户连接, 用户状态) 20 RpcGatewayClientExample() 21 22 // 发布订阅(群聊) 23 RpcSubscriberClientExample() 24 } 25 26 func RpcGatewayClientUpdateClient() { 27 28 options := &rpc.ClientOptions{ 29 Addr: "127.0.0.1", 30 Port: 8092, 31 Name: "im_rpc_server", 32 } 33 cli, err := client.NewGatewayRpcImpl(options) 34 35 defer cli.Close() 36 if err != nil { 37 panic(err) 38 } 39 40 err = cli.UpdateClient(gate.NewID2("544607"), &gate.ClientSecrets{MessageDeliverSecret: "secret"}) 41 //err = cli.EnqueueMessage(gate.NewID2("544607"), messages.NewEmptyMessage()) 42 //err = cli.ExitClient(gate.NewID2("544607")) 43 if err != nil { 44 panic(err) 45 } 46 } 47 48 func RpcGatewayClientExample() { 49 50 // 消息网关 RPC 客户端配置 51 options := &rpc.ClientOptions{ 52 Addr: "127.0.0.1", 53 Port: 8092, 54 Name: "im_rpc_server", 55 } 56 57 // 创建消息网关接口客户端 58 cli, err := client.NewGatewayRpcImpl(options) 59 // 长时间不用完记得关闭 60 defer cli.Close() 61 if err != nil { 62 panic(err) 63 } 64 65 // 给网关中指定 id 的链接推送一条消息 (例如加好友通知, 多设备登录通知等等) 66 err = cli.EnqueueMessage(gate.NewID2("1"), messages.NewEmptyMessage()) 67 if err != nil { 68 panic(err) 69 } 70 71 // 设置网关中连接新 id 72 err = cli.SetClientID(gate.NewID2("1"), gate.NewID2("2")) 73 if err != nil { 74 panic(err) 75 } 76 77 // 断开 uid 为 1 的设备 1 78 // 单体情况, 网关 id 传空即可 79 _ = cli.ExitClient(gate.NewID("", "1", "1")) 80 } 81 82 func RpcSubscriberClientExample() { 83 options := &rpc.ClientOptions{ 84 Addr: "127.0.0.1", 85 Port: 8092, 86 Name: "im_rpc_server", 87 } 88 cli, err := client.NewSubscriptionRpcImpl(options) 89 defer cli.Close() 90 if err != nil { 91 panic(err) 92 } 93 94 //err = cli.CreateChannel("1", &subscription.ChanInfo{ 95 // ID: "1", 96 // Type: 0, 97 //}) 98 //if err != nil { 99 // panic(err) 100 //} 101 102 // 用户订阅某个频道的消息(用户上线, 开始接受群消息) 103 err = cli.Subscribe("1", "1", &subscription_impl.SubscriberOptions{ 104 Perm: subscription_impl.PermRead | subscription_impl.PermWrite, 105 }) 106 if err != nil { 107 panic(err) 108 } 109 110 // 移除指定 id 频道 (解散群, 删除频道等) 111 _ = cli.RemoveChannel("1") 112 113 msg := &subscription_impl.PublishMessage{ 114 From: "1", 115 Seq: 1, 116 Type: subscription_impl.TypeMessage, 117 Message: messages.NewMessage(0, "1", &messages.ChatMessage{}), 118 } 119 // 推送消息到指定频道 (发送一条系统消息, 群通知等) 120 err = cli.Publish("1", msg) 121 if err != nil { 122 panic(err) 123 } 124 }