/
im_server.go
140 lines (119 loc) · 2.96 KB
/
im_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
*
* im_server.go
* server
*
* Created by lintao on 2020/4/16 2:20 下午
* Copyright © 2020-2020 LINTAO. All rights reserved.
*
*/
package tim
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"strconv"
"time"
sign2 "github.com/tencentyun/tls-sig-api-v2-golang/tencentyun"
)
type IMServer struct {
AppId int
Identifier string
SecretKey string
Expire int
Sig string
}
func NewIMServer(appId, expire int, identifier, secretKey string, opts ...ServerOption) (IMServer, error) {
server := IMServer{
AppId: appId,
Identifier: identifier,
SecretKey: secretKey,
Expire: expire,
}
var err error
if server.Sig, err = server.userSig(); err != nil {
return IMServer{}, err
}
for _, opt := range opts {
if err := opt.SetOption(&server); err != nil {
return IMServer{}, err
}
}
return server, nil
}
func (s IMServer) ListenCallback() {
Run(strconv.Itoa(s.AppId))
}
func (s IMServer) request(url string, requestJson []byte) ([]byte, error) {
body := bytes.NewBuffer(requestJson)
// Create client
log.Println(string(requestJson))
client := &http.Client{}
// Create request
req, err := http.NewRequest("POST", url, body)
if err != nil {
log.Panic(err)
}
// Headers
req.Header.Add("Content-Type", "application/json; charset=utf-8")
// Fetch Request
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer func(){
if err:=resp.Body.Close();err!=nil{
log.Println("response body close error:",err)
}
}()
// Read Response Body
respBody, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http erro status code is %d", resp.StatusCode)
}
var respCheck struct {
ErrorInfo string `json:"ErrorInfo"`
ErrorCode int `json:"ErrorCode"`
}
if err = json.Unmarshal(respBody, &respCheck); err != nil {
return nil, err
}
if respCheck.ErrorCode != 0 {
return nil, fmt.Errorf("操作失败,错误码 %d ,错误信息 %s \n 详情请查询 "+
"https://cloud.tencent.com/document/product/269/1671", respCheck.ErrorCode, respCheck.ErrorInfo)
}
log.Println(string(respBody))
return respBody, nil
}
func (s IMServer) requestWithPath(path ApiPath, v interface{}) (jsons []byte, err error) {
var b []byte
switch a := v.(type) {
case []byte:
b = a
default:
b, err = json.Marshal(&v)
if err != nil {
return nil, err
}
}
return s.request(s.combineURL(path), b)
}
func (s IMServer) userSig() (string, error) {
userSig, err := sign2.GenUserSig(s.AppId, s.SecretKey, s.Identifier, s.Expire)
if err != nil {
return "", err
}
return userSig, nil
}
func (s IMServer) combineURL(path ApiPath) string {
rand.Seed(time.Now().Unix())
return fmt.Sprintf("%s/%s%s?sdkappid=%d&identifier=%s&usersig=%s&random=%d&contenttype=json",
BaseUrl, VERSION, path, s.AppId, s.Identifier, s.Sig, rand.Intn(4294967294))
}
func (s IMServer) Do(path ApiPath, v interface{}) (jsons []byte, err error) {
return s.requestWithPath(path,v)
}