forked from link1st/gowebsocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
shuchenchen
committed
Jul 29, 2019
1 parent
7aa22cc
commit ebae55c
Showing
656 changed files
with
327,097 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Link | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
### goWebSocket | ||
golang websocket websocket 中间键,单机支持百万连接,使用gin框架、nginx负载、可以水平部署、程序内部相互通讯、使用grpc通讯协议 | ||
|
||
### 架构图 | ||
|
||
|
||
### QA | ||
|
||
|
||
一个用户,多个平台同时登陆 | ||
发消息的时候选择一个平台发送 | ||
|
||
|
||
### js发送消息 | ||
```$xslt | ||
ws = new WebSocket("ws://127.0.0.1:8089/acc"); | ||
// setTimeout(时间,"JS代码"); | ||
ws.onopen = function(evt) { | ||
console.log("Connection open ..."); | ||
}; | ||
ws.onmessage = function(evt) { | ||
console.log( "Received Message: " + evt.data); | ||
data_array = JSON.parse(evt.data); | ||
console.log( data_array); | ||
}; | ||
ws.onclose = function(evt) { | ||
console.log("Connection closed."); | ||
}; | ||
``` | ||
|
||
### 发送数据 | ||
```$xslt | ||
登录: | ||
ws.send('{"seq":"2323","cmd":"login","data":{"userId":"11"}}'); | ||
心跳: | ||
ws.send('{"seq":"2323","cmd":"heartbeat","data":{}}'); | ||
关闭连接: | ||
ws.close(); | ||
``` | ||
|
||
#### 待办事项 | ||
- 读取配置文件 | ||
- 定时脚本,清理过期未心跳链接 | ||
- http接口,获取登录、链接数量 | ||
- grpc 程序内部通讯,发送消息 | ||
- http接口,发送push、查询有多少人在线 | ||
- appIds 一个用户在多个平台登录 | ||
- 界面,把所有在线的人拉倒一个群里面,发送消息 | ||
- 群聊、单聊 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Created by GoLand. | ||
* User: link1st | ||
* Date: 2019-07-25 | ||
* Time: 12:11 | ||
*/ | ||
|
||
package common | ||
|
||
const ( | ||
OK = 200 // Success | ||
NotLoggedIn = 1000 // 未登录 | ||
ParameterIllegal = 1001 // 参数不合法 | ||
UnauthorizedUserId = 1002 // 非法的用户Id | ||
Unauthorized = 1003 // 未授权 | ||
ServerError = 1004 // 系统错误 | ||
NotData = 1005 // 没有数据 | ||
ModelAddError = 1006 // 添加错误 | ||
ModelDeleteError = 1007 // 删除错误 | ||
ModelStoreError = 1008 // 存储错误 | ||
OperationFailure = 1009 // 操作失败 | ||
RoutingNotExist = 1010 // 路由不存在 | ||
) | ||
|
||
// 根据错误码 获取错误信息 | ||
func GetErrorMessage(code int, message string) string { | ||
var codeMessage string | ||
codeMap := map[int]string{ | ||
OK: "Success", | ||
NotLoggedIn: "未登录", | ||
ParameterIllegal: "参数不合法", | ||
UnauthorizedUserId: "非法的用户Id", | ||
Unauthorized: "未授权", | ||
NotData: "没有数据", | ||
ServerError: "系统错误", | ||
ModelAddError: "添加错误", | ||
ModelDeleteError: "删除错误", | ||
ModelStoreError: "存储错误", | ||
OperationFailure: "操作失败", | ||
RoutingNotExist: "路由不存在", | ||
} | ||
|
||
if message == "" { | ||
if value, ok := codeMap[code]; ok { | ||
// 存在 | ||
codeMessage = value | ||
} else { | ||
codeMessage = "未定义错误类型!" | ||
} | ||
} else { | ||
codeMessage = message | ||
} | ||
|
||
return codeMessage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Created by GoLand. | ||
* User: link1st | ||
* Date: 2019-07-25 | ||
* Time: 12:11 | ||
*/ | ||
|
||
package common | ||
|
||
type JsonResult struct { | ||
Code int `json:"code"` | ||
Msg string `json:"msg"` | ||
Data interface{} `json:"data"` | ||
} | ||
|
||
func Response(code int, message string, data interface{}) JsonResult { | ||
|
||
message = GetErrorMessage(code, message) | ||
jsonMap := grantMap(code, message, data) | ||
|
||
return jsonMap | ||
} | ||
|
||
// 按照接口格式生成原数据数组 | ||
func grantMap(code int, message string, data interface{}) JsonResult { | ||
|
||
jsonMap := JsonResult{ | ||
Code: code, | ||
Msg: message, | ||
Data: data, | ||
} | ||
return jsonMap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
app: | ||
logFile: log/gin.log | ||
httpPort: 8080 | ||
webSocketPort: 8089 | ||
|
||
|
||
redis: | ||
addr: "localhost:6379" | ||
password: "" | ||
DB: 0 | ||
poolSize: 30 | ||
minIdleConns: 30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Created by GoLand. | ||
* User: link1st | ||
* Date: 2019-07-25 | ||
* Time: 12:11 | ||
*/ | ||
|
||
package controllers | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"gowebsocket/common" | ||
"net/http" | ||
) | ||
|
||
type BaseController struct { | ||
gin.Context | ||
} | ||
|
||
// 获取全部请求解析到map | ||
func Response(c *gin.Context, code int, msg string, data map[string]interface{}) { | ||
message := common.Response(code, msg, data) | ||
c.JSON(http.StatusOK, message) | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Created by GoLand. | ||
* User: link1st | ||
* Date: 2019-07-25 | ||
* Time: 14:29 | ||
*/ | ||
|
||
package test | ||
|
||
import "github.com/gin-gonic/gin" | ||
|
||
// 获取连接信息 | ||
func GetAccLink(c *gin.Context) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Created by GoLand. | ||
* User: link1st | ||
* Date: 2019-07-25 | ||
* Time: 12:11 | ||
*/ | ||
|
||
package user | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
"gowebsocket/common" | ||
"gowebsocket/controllers" | ||
) | ||
|
||
// 查看用户是否在线 | ||
func Online(c *gin.Context) { | ||
|
||
// 获取参数 | ||
name := c.DefaultPostForm("name", "") | ||
fmt.Println("Hello", name) | ||
|
||
data := make(map[string]interface{}) | ||
|
||
data["name"] = name | ||
|
||
controllers.Response(c, common.OK, "", data) | ||
} | ||
|
||
// 给用户发送消息 | ||
func SendMessage(c *gin.Context) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Created by GoLand. | ||
* User: link1st | ||
* Date: 2019-07-25 | ||
* Time: 17:27 | ||
*/ | ||
|
||
package helper | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
// 获取服务器Ip | ||
func GetServerIp() (ip string) { | ||
addrs, err := net.InterfaceAddrs() | ||
|
||
if err != nil { | ||
return "" | ||
} | ||
|
||
for _, address := range addrs { | ||
// 检查ip地址判断是否回环地址 | ||
if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() { | ||
if ipNet.IP.To4() != nil { | ||
ip = ipNet.IP.String() | ||
} | ||
} | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Created by GoLand. | ||
* User: link1st | ||
* Date: 2019-07-26 | ||
* Time: 09:18 | ||
*/ | ||
|
||
package cache | ||
|
||
import ( | ||
"fmt" | ||
"gowebsocket/lib/redislib" | ||
) | ||
|
||
const ( | ||
submitAgainPrefix = "acc:submit:again:" // 数据不重复提交 | ||
) | ||
|
||
/********************* 查询数据是否处理过 ************************/ | ||
|
||
// 获取数据提交去除key | ||
func getSubmitAgainKey(from string, value string) (key string) { | ||
key = fmt.Sprintf("%s%s:%s", submitAgainPrefix, from, value) | ||
|
||
return | ||
} | ||
|
||
// 重复提交 | ||
// return true:重复提交 false:第一次提交 | ||
func submitAgain(from string, second int, value string) (isSubmitAgain bool) { | ||
|
||
// 默认重复提交 | ||
isSubmitAgain = true | ||
key := getSubmitAgainKey(from, value) | ||
|
||
redisClient := redislib.GetClient() | ||
number, err := redisClient.Do("setNx", key, "1").Int() | ||
if err != nil { | ||
fmt.Println("submitAgain", key, number, err) | ||
|
||
return | ||
} | ||
|
||
if number != 1 { | ||
|
||
return | ||
} | ||
// 第一次提交 | ||
isSubmitAgain = false | ||
|
||
redisClient.Do("Expire", key, second) | ||
|
||
return | ||
|
||
} | ||
|
||
// Seq 重复提交 | ||
func SeqDuplicates(seq string) (result bool) { | ||
result = submitAgain("seq", 12*60*60, seq) | ||
|
||
return | ||
} |
Oops, something went wrong.