代码拉取完成,页面将自动刷新
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package main
import (
"context"
"fmt"
"io"
"time"
"gitee.com/clannad_sk/webkit/v2/frame/g"
"gitee.com/clannad_sk/webkit/v2/net/gtcp"
"gitee.com/clannad_sk/webkit/v2/os/gctx"
"gitee.com/clannad_sk/webkit/v2/os/gtime"
"gitee.com/clannad_sk/webkit/v2/os/gtimer"
)
const (
AddressOfServer1 = ":8198"
AddressOfServer2 = ":8199"
UpStream = "127.0.0.1:8198"
)
var (
ctx = gctx.GetInitCtx()
)
// StartTCPServer1 starts Server1: A simple tcp server for demo.
// It reads the content from client connect and write it back to client.
func StartTCPServer1() {
s := g.TCPServer(1)
s.SetHandler(func(conn *gtcp.Conn) {
defer conn.Close()
for {
data, err := conn.Recv(-1)
if err != nil {
g.Log().Errorf(ctx, `%+v`, err)
break
}
if len(data) > 0 {
err = conn.Send([]byte(fmt.Sprintf(`received: %s`, data)))
if err != nil {
g.Log().Errorf(ctx, `%+v`, err)
break
}
}
}
})
s.SetAddress(AddressOfServer1)
s.Run()
}
// StartTCPServer2 starts Server2:
// All requests to Server2 are directly redirected to Server1.
func StartTCPServer2() {
s := g.TCPServer(2)
s.SetHandler(func(conn *gtcp.Conn) {
defer conn.Close()
// Each client connection associates an upstream connection.
upstreamClient, err := gtcp.NewConn(UpStream)
if err != nil {
_, _ = conn.Write([]byte(fmt.Sprintf(
`cannot connect to upstream "%s": %s`, UpStream, err.Error(),
)))
return
}
// Redirect the client connection reading and writing to upstream connection.
for {
go io.Copy(upstreamClient, conn)
_, err = io.Copy(conn, upstreamClient)
if err != nil {
_, _ = conn.Write([]byte(fmt.Sprintf(
`io.Copy to upstream "%s" failed: %s`, UpStream, err.Error(),
)))
}
}
})
s.SetAddress(AddressOfServer2)
s.Run()
}
func main() {
go StartTCPServer1()
go StartTCPServer2()
time.Sleep(time.Second)
gtimer.Add(ctx, time.Second, func(ctx context.Context) {
address := fmt.Sprintf(`127.0.0.1%s`, AddressOfServer2)
result, err := gtcp.SendRecv(address, []byte(gtime.Now().String()), -1)
if err != nil {
g.Log().Errorf(ctx, `send data failed: %+v`, err)
}
g.Log().Info(ctx, result)
})
g.Listen()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。