赞
踩
环信 Server SDK 是对环信 IM REST API 的封装, 可以节省服务器端开发者对接环信 API 的时间,只需要配置自己的 App Key 相关信息即可使用。
环信目前提供java和PHP版本的Server SDK,此项目使用go语言对环信 IM REST API 进行封装,对官方版本进行了补充,有需要的开发者可以直接通过以下地址获取源码。
go语言环境
有效的环信即时通讯 IM 开发者账号和 AppKey、ClientID、ClientSecret、DomainURL
登录 环信管理后台 到“应用列表” → 点击“查看”即可获取到 App Key、Client ID、ClientSecret,到"即时通讯" → 点击"服务概览"获取到 “Rest api” 的服务器域名。
AppKey、ClientID、ClientSecret在下图中获取
DomainURL在下图中获取
package main import ( "context" "fmt" IMSDK "github.com/xiaofengin/easemob-go" ) func main() { client, err := IMSDK.New("appkey", "clientId", "clientSecret", "domainURL") if err != nil { return } }
package main import ( "context" "fmt" IMSDK "github.com/xiaofengin/easemob-go" ) func main() { client, err := IMSDK.New("appkey", "clientId", "clientSecret", "domainURL") if err != nil { return } user1 := UserRegisterParam{ Username: "userID_1", Password: "1", } user2 := UserRegisterParam{ Username: "userID_2", Password: "1", } users := []UserRegisterParam{user1, user2} ret, err := client.UserRegister(context.Background(), &users) if err != nil { return } fmt.Printf("数据的值:%v\n", ret.Entities) }
m := CreateTextMsg("hello word", tos) 创建一个消息体
m.From = "指定ID"
m.Ext = map[string]interface{}{"key1": "value1", "key2": "value2"}
package main import ( "context" "fmt" IMSDK "github.com/xiaofengin/easemob-go" ) func main() { client, err := IMSDK.New("appkey", "clientId", "clientSecret", "domainURL") if err != nil { return } var tos []string tos = append(tos, "环信用户ID") m := CreateTextMsg("hello word", tos) //m.From = "指定ID" //m.Ext = map[string]interface{}{"key1": "value1", "key2": "value2"} ret, err := client.SendChatMessage(context.Background(), m) if err != nil { return } fmt.Printf("数据的值:%v\n", ret.Data) }
package main import ( "context" "fmt" IMSDK "github.com/xiaofengin/easemob-go" ) func main() { client, err := IMSDK.New("appkey", "clientId", "clientSecret", "domainURL") if err != nil { return } //通过用户 ID 和密码获取用户 token //data := TokenParam{ // GrantType: "password", // Username: "userID", // Password: "1", // Ttl: "1024000", //} //通过用户 ID 获取用户 token data := TokenParam{ GrantType: "inherit", Username: "userID", AutoCreateUser: true, Ttl: "1024000", } ret, err := client.GetUserToken(context.Background(), &data) if err != nil { return } fmt.Printf("数据的值:%v\n", ret.AccessToken) }
功能 | 地址 |
---|---|
user 用户信息模块 | https://github.com/xiaofengin/easemob-go/blob/main/user_test.go |
push 推送信息模块 | https://github.com/xiaofengin/easemob-go/blob/main/push_test.go |
message 消息模块 | https://github.com/xiaofengin/easemob-go/blob/main/message_test.go |
contact 好友模块 | https://github.com/xiaofengin/easemob-go/blob/main/contact_test.go |
chatroom 聊天室模块 | https://github.com/xiaofengin/easemob-go/blob/main/chatroom_test.go |
chatgroup 群组模块 | https://github.com/xiaofengin/easemob-go/blob/main/chatgroup_test.go |
测试代码中 appkey clientId clientSecret 这三个参数我是写到环境变量里面,
如果 你没有把参数写到环境变量里面,可以直接写死该参数
注册环信IM:https://console.easemob.com/user/register
环信SDK下载:https://www.easemob.com/download/im
GO版本Server SDK: https://github.com/xiaofengin/easemob-go
IMGeek社区支持:https://www.imgeek.net
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。