TODO: think

This commit is contained in:
eson 2021-04-27 18:36:08 +08:00
parent a7f0eabbc7
commit 6d1b058de8
10 changed files with 214 additions and 49 deletions

2
.vscode/launch.json vendored
View File

@ -24,7 +24,7 @@
"--disable-features=TranslateUI",
"--test-type",
],
"url": "http:/localhost:7123",
"url": "http:/localhost:12203/start",
"webRoot": "${workspaceFolder}"
}
]

View File

@ -2,7 +2,8 @@ console.log("background");
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
console.log(request, sender)
sendResponse({});
// sendResponse({});
});

View File

@ -1,14 +1,43 @@
// var href = window.location.href;
if( window.location.href === "http://localhost:12203/start") {
var ws = new WebSocket("ws://localhost:12203/task");
ws.onopen = function(evt) {
console.log("Connection open ...", evt);
ws.send("Hello WebSockets!");
};
ws.onmessage = function(evt) {
console.log("Received Message: ", JSON.parse(evt.data));
// if (window.location.hostname !== "www.zhipin.com") {
// var handler = window.open("https://www.zhipin.com/")
// }
chrome.runtime.sendMessage(JSON.parse(evt.data))
};
ws.onclose = function(evt) {
console.log("Connection closed.");
};
ws.onerror = function(evt) {
console.log(evt);
}
}
setInterval(function name() {
chrome.runtime.sendMessage({say: "Hello"})
fetch("http://localhost:12203/api").then((response)=>{
console.log(response);
console.log(response.json())
})
}, 10000)
// setInterval(function name() {
// chrome.runtime.sendMessage({say: "Hello"})
// fetch("http://localhost:12203/api").then((response)=>{
// console.log(response);
// console.log(response.json())
// })
// }, 10000)
// if(href.startsWith(Host)) {
// setInterval(function(){

28
server/.gitignore vendored Normal file
View File

@ -0,0 +1,28 @@
# Created by https://www.toptal.com/developers/gitignore/api/go
# Edit at https://www.toptal.com/developers/gitignore?templates=go
### Go ###
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
### Go Patch ###
/vendor/
/Godeps/
# End of https://www.toptal.com/developers/gitignore/api/go
server

12
server/case_test.go Normal file
View File

@ -0,0 +1,12 @@
package main
import (
"testing"
"github.com/474420502/focus/compare"
pqueuekey "github.com/474420502/focus/priority_queuekey"
)
func Benchmark123(b *testing.B) {
pqueuekey.New(compare.Int)
}

View File

@ -2,4 +2,8 @@ module server
go 1.16
require github.com/gin-gonic/gin v1.7.1
require (
github.com/474420502/focus v0.13.3 // indirect
github.com/gin-gonic/gin v1.7.1
github.com/gorilla/websocket v1.4.2
)

View File

@ -1,3 +1,6 @@
github.com/474420502/focus v0.13.3 h1:fiP6HNSfOukx76xUovKG5WzswMysHF7A5xhyrcYCrLU=
github.com/474420502/focus v0.13.3/go.mod h1:SrqNq63qSx53TkvVWOuHTbTSvAQTNO8ZUrufXm0Ncq8=
github.com/Pallinder/go-randomdata v1.1.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -16,6 +19,8 @@ github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
@ -46,6 +51,7 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtD
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -1,56 +1,68 @@
package main
import (
"encoding/json"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
// Cors 跨域中间间
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method //请求方法
origin := c.Request.Header.Get("Origin") //请求头部
// var headerKeys []string // 声明请求头keys
// for k := range c.Request.Header {
// headerKeys = append(headerKeys, k)
// }
// headerStr := strings.Join(headerKeys, ", ")
// if headerStr != "" {
// headerStr = fmt.Sprintf("access-control-allow-origin, access-control-allow-headers, %s", headerStr)
// } else {
// headerStr = "access-control-allow-origin, access-control-allow-headers"
// }
if origin != "" {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Origin", "*") // 这是允许访问所有域
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE") //服务器支持的所有跨域请求的方法,为了避免浏览次请求的多次'预检'请求
// header的类型
c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session,X_Requested_With,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language,DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma")
// 允许跨域设置 可以返回其他子段
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar") // 跨域关键设置 让浏览器可以解析
c.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
c.Header("Access-Control-Allow-Credentials", "true") // 跨域请求是否需要带cookie信息 默认设置为true
c.Set("content-type", "application/json") // 设置返回格式是json
}
//放行所有OPTIONS方法
if method == "OPTIONS" {
c.JSON(http.StatusOK, "Options Request!")
}
// 处理请求
c.Next() // 处理请求
}
var upGrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func main() {
eg := gin.Default()
eg.Use(Cors())
eg.GET("api", func(c *gin.Context) {
eg.GET("start", func(c *gin.Context) {
c.JSON(0, gin.H{
"api": "ok",
"task": "ok",
})
})
eg.GET("task", func(c *gin.Context) {
//升级get请求为webSocket协议
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
return
}
defer ws.Close()
mt, message, err := ws.ReadMessage()
if err != nil {
c.Error(err)
return
}
for {
//读取ws中的数据
log.Println(
"123",
)
msg := gin.H{
"content": string(message),
}
data, err := json.Marshal(msg)
if err != nil {
log.Println(err)
}
//写入ws数据
err = ws.WriteMessage(mt, data)
if err != nil {
c.Error(err)
break
}
time.Sleep(time.Second * 10)
}
})
eg.Run(":12203")
}

45
server/middleware.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Cors 跨域中间间
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method //请求方法
origin := c.Request.Header.Get("Origin") //请求头部
// var headerKeys []string // 声明请求头keys
// for k := range c.Request.Header {
// headerKeys = append(headerKeys, k)
// }
// headerStr := strings.Join(headerKeys, ", ")
// if headerStr != "" {
// headerStr = fmt.Sprintf("access-control-allow-origin, access-control-allow-headers, %s", headerStr)
// } else {
// headerStr = "access-control-allow-origin, access-control-allow-headers"
// }
if origin != "" {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Origin", "*") // 这是允许访问所有域
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE") //服务器支持的所有跨域请求的方法,为了避免浏览次请求的多次'预检'请求
// header的类型
c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session,X_Requested_With,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language,DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma")
// 允许跨域设置 可以返回其他子段
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar") // 跨域关键设置 让浏览器可以解析
c.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
c.Header("Access-Control-Allow-Credentials", "true") // 跨域请求是否需要带cookie信息 默认设置为true
c.Set("content-type", "application/json") // 设置返回格式是json
}
//放行所有OPTIONS方法
if method == "OPTIONS" {
c.JSON(http.StatusOK, "Options Request!")
}
// 处理请求
c.Next() // 处理请求
}
}

28
server/task.go Normal file
View File

@ -0,0 +1,28 @@
package main
import (
"sync"
pqueuekey "github.com/474420502/focus/priority_queuekey"
"github.com/gin-gonic/gin"
)
type Task struct {
}
type Queue struct {
lock sync.Mutex
count uint64
queue *pqueuekey.PriorityQueue
}
func (q *Queue) Push(task *Task) {
q.lock.Lock()
defer q.lock.Unlock()
q.count++
q.queue.Push(q.count, task)
}
func PushQueue(c *gin.Context) {
}