Compare commits
No commits in common. "20cc0d355f4f4e12da60bc6eac84f85300c1eee3" and "87c52f79a4c041675056ba4290119e82b1ae327b" have entirely different histories.
20cc0d355f
...
87c52f79a4
1
go.mod
1
go.mod
|
@ -4,7 +4,6 @@ go 1.16
|
|||
|
||||
require (
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
|
||||
github.com/gorilla/websocket v1.5.0
|
||||
github.com/howeyc/fsnotify v0.9.0 // indirect
|
||||
github.com/idoubi/goutils v1.3.2 // indirect
|
||||
github.com/idoubi/goz v1.4.4 // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -13,8 +13,6 @@ github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keL
|
|||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
||||
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/howeyc/fsnotify v0.9.0 h1:0gtV5JmOKH4A8SsFxG2BczSeXWWPvcMT0euZt5gDAxY=
|
||||
github.com/howeyc/fsnotify v0.9.0/go.mod h1:41HzSPxBGeFRQKEEwgh49TRw/nKBsYZ2cF1OzPjSJsA=
|
||||
github.com/idoubi/goutils v1.1.0/go.mod h1:BVikG2hf3aDWXsBzBw3X7l4jSuuxNQQVgrxQ6amIAOk=
|
||||
|
|
82
main.go
82
main.go
|
@ -1,49 +1,67 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/idoubi/goz"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"golang.org/x/net/websocket"
|
||||
)
|
||||
|
||||
func ws(c echo.Context) error {
|
||||
websocket.Handler(server).ServeHTTP(c.Response(), c.Request())
|
||||
return nil
|
||||
}
|
||||
func add(c echo.Context) error {
|
||||
tip_number++
|
||||
return c.String(http.StatusOK, strconv.Itoa(tip_number))
|
||||
type server struct {
|
||||
// 通过json标签指定json字段名
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
func server(ws *websocket.Conn) {
|
||||
defer ws.Close()
|
||||
fmt.Printf("new connection\n")
|
||||
for {
|
||||
if tip_number == 0 {
|
||||
continue
|
||||
type result struct {
|
||||
// 通过json标签指定json字段名
|
||||
Sataus int `json:"sataus"`
|
||||
Code int `json:"code"`
|
||||
Time int `json:"time"`
|
||||
}
|
||||
var strr string = strconv.Itoa(tip_number)
|
||||
err := websocket.Message.Send(ws, strr)
|
||||
if err != nil {
|
||||
fmt.Print(err)
|
||||
}
|
||||
tip_number = 0
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var tip_number int = 0
|
||||
|
||||
func main() {
|
||||
e := echo.New()
|
||||
e.Use(middleware.Logger())
|
||||
e.Use(middleware.Recover())
|
||||
e.Static("/", "./public")
|
||||
e.GET("/ws", ws)
|
||||
e.GET("/add", add)
|
||||
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{AllowOrigins: []string{"*"}, AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE}}))
|
||||
e.GET("/", func(c echo.Context) error {
|
||||
return c.String(http.StatusOK, "Hello, World!")
|
||||
})
|
||||
|
||||
e.POST("/textserver", textserver)
|
||||
e.Logger.Fatal(e.Start(":1323"))
|
||||
}
|
||||
|
||||
func textserver(c echo.Context) error {
|
||||
|
||||
// 初始化接收json参数的对象
|
||||
u := new(server)
|
||||
// 通过Bind将json参数绑定到struct对象
|
||||
if err := c.Bind(u); err != nil {
|
||||
return err
|
||||
}
|
||||
// 通过结构体对象就可以访问json参数
|
||||
json := new(result)
|
||||
json.Code = http.StatusOK
|
||||
start := time.Now().UnixMilli()
|
||||
json.Sataus = text_url(u.Url)
|
||||
elapsed := time.Now().UnixMilli() - start
|
||||
json.Time = int(elapsed)
|
||||
return c.JSON(http.StatusOK, json)
|
||||
}
|
||||
func text_url(url string) int {
|
||||
cli := goz.NewClient(goz.Options{
|
||||
Timeout: 2,
|
||||
})
|
||||
|
||||
resp, err := cli.Get(url)
|
||||
if err != nil {
|
||||
if resp.IsTimeout() {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
return resp.GetStatusCode()
|
||||
}
|
||||
func setAccessOriginUrl(c echo.Context) {
|
||||
c.Response().Header().Set("Access-Control-Allow-Origin", "*")
|
||||
}
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>WebSocket</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<p id="output"></p>
|
||||
|
||||
<script>
|
||||
var loc = window.location;
|
||||
var uri = 'ws:';
|
||||
|
||||
if (loc.protocol === 'https:') {
|
||||
uri = 'wss:';
|
||||
}
|
||||
uri += '//' + loc.host;
|
||||
uri += loc.pathname + 'ws';
|
||||
|
||||
ws = new WebSocket(uri)
|
||||
|
||||
ws.onopen = function() {
|
||||
console.log('Connected')
|
||||
}
|
||||
|
||||
ws.onmessage = function(evt) {
|
||||
var out = document.getElementById('output');
|
||||
out.innerHTML += evt.data + '<br>';
|
||||
}
|
||||
|
||||
// setInterval(function() {
|
||||
// ws.send('Hello, Server!');
|
||||
// }, 1000);
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in New Issue