项目背景
环信 Server SDK 是对环信 IM REST API 的封装, 可以节省服务器端开发者对接环信 API 的时间,只需要配置自己的 App Key 相关信息即可使用。
环信目前提供java和PHP版本的Server SDK,此项目使用go语言对环信 IM REST API 进行封装,对官方版本进行了补充,有需要的开发者可以直接通过以下地址获取源码。
项目地址
- https://github.com/xiaofengin/easemob-go
- 如果有任何问题可以通过 issue 的形式反馈,作者会积极更新该仓库
前提条件
-
go语言环境
-
有效的环信即时通讯 IM 开发者账号和 AppKey、ClientID、ClientSecret、DomainURL
登录 环信管理后台 到“应用列表” → 点击“查看”即可获取到 App Key、Client ID、ClientSecret,到"即时通讯" → 点击"服务概览"获取到 “Rest api” 的服务器域名。
实现方法
- go.mod 文件引入: github.com/xiaofengin/easemob-go
AppKey、ClientID、ClientSecret在下图中获取
DomainURL在下图中获取
初始化IM SDK
package mainimport ("context""fmt"IMSDK "github.com/xiaofengin/easemob-go"
)func main() {client, err := IMSDK.New("appkey","clientId","clientSecret","domainURL")if err != nil {return}
}
批量注册两个用户
package mainimport ("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)
}
发送一个单聊消息
- tos 放接收方环信ID(多个)
m := CreateTextMsg("hello word", tos) 创建一个消息体
- 默认发送方ID 是 admin,如果要修改的话
m.From = "指定ID"
- 也可以给消息添加扩展字段
m.Ext = map[string]interface{}{"key1": "value1", "key2": "value2"}
package mainimport ("context""fmt"IMSDK "github.com/xiaofengin/easemob-go"
)func main() {client, err := IMSDK.New("appkey","clientId","clientSecret","domainURL")if err != nil {return}var tos []stringtos = 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)
}
获取用户token
- 通过用户ID和密码获取用户token,也可以通过用户ID获取用户token
package mainimport ("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 获取用户 tokendata := 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)
}
SDK功能清单
功能 | 地址 |
---|---|
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