咸宁市做网站,自助建站 平台,自媒体账号,app开发公司办公室设计项目地址
https://github.com/cmdch2017/http_tcpServer
项目结构 如何快速上手
http篇
1、controller包就相当于RestController#xff0c;这里返回了一个Person对象#xff0c;当你需要新建一个接口时#xff0c;再新写一个func仿照下面的方法就行了
package control…项目地址
https://github.com/cmdch2017/http_tcpServer
项目结构 如何快速上手
http篇
1、controller包就相当于RestController这里返回了一个Person对象当你需要新建一个接口时再新写一个func仿照下面的方法就行了
package controllerimport (net/httptestproject12/commontestproject12/entitytestproject12/util
)// PersonHandler 无入参返回person类
func PersonHandler(w http.ResponseWriter, r *http.Request) {// 创建一个Person对象person : entity.Person{Name: John Doe,Age: 30,Title: Software Engineer,}messageHex : FFFFFFutil.SendHexMessageToAllUser(messageHex)// 封装为JSON格式的CommonResult并写入响应体common.WriteJSONResponse(w, http.StatusOK, common.DefaultSuccessMsg, person)
}
2、router.go负责配置路由下面的代码中server.AddRoute的key相当于RequestMapping(“/person”)value是上面文件在controller包下方法是PersonHandler
package routerimport (testproject12/controllertestproject12/httpServer
)func SetupRoutes(server *httpServer.HTTPServer) {// 添加路由server.AddRoute(/person, controller.PersonHandler)// Add more routes as needed
} tcp篇
用SocketTool新建一个客户端 http://localhost:8080/person 服务器收到数据FFFFFF
总结
实现了http请求与tcp请求如果有需要的话请fork我的github代码
性能 附录
完整代码
package commonimport (encoding/jsonfmtnet/http
)// Constants for CommonResult keys
const (CodeTag codeMsgTag msgDataTag data
)// Default success and failure messages
const (DefaultSuccessMsg 操作成功DefaultFailedMsg 操作失败
)// CommonResult creates a new CommonResult object with the given code and message
func CommonResult(code int, msg string) map[string]interface{} {return map[string]interface{}{CodeTag: code,MsgTag: msg,}
}func CommonResultWithData(code int, msg string, data interface{}) map[string]interface{} {result : CommonResult(code, msg)if data ! nil {result[DataTag] data}return result
}// Success returns a CommonResult representing success with an optional data payload
func Success(data ...interface{}) map[string]interface{} {result : CommonResult(HttpStatusSuccess, DefaultSuccessMsg)if len(data) 0 {result[DataTag] data[0]}return result
}// Error returns a CommonResult representing an error with an optional data payload
func Error(msg string, data ...interface{}) map[string]interface{} {result : CommonResult(HttpStatusError, msg)if len(data) 0 {result[DataTag] data[0]}return result
}// HttpStatus constants
const (HttpStatusSuccess iotaHttpStatusError
)func WriteJSONResponse(w http.ResponseWriter, code int, msg string, data interface{}) {// 创建一个CommonResult并将数据放入其中result : CommonResultWithData(code, msg, data)// 将CommonResult对象序列化为JSON格式的字符串resultData, err : json.Marshal(result)if err ! nil {fmt.Println(json格式化错误)http.Error(w, Internal Server Error, http.StatusInternalServerError)return}// 设置响应头为JSON格式w.Header().Set(Content-Type, application/json)// 将JSON数据写入响应体w.Write(resultData)
}
package controllerimport (net/httptestproject12/commontestproject12/entitytestproject12/util
)// PersonHandler 无入参返回person类
func PersonHandler(w http.ResponseWriter, r *http.Request) {// 创建一个Person对象person : entity.Person{Name: John Doe,Age: 30,Title: Software Engineer,}messageHex : FFFFFFutil.SendHexMessageToAllUser(messageHex)// 封装为JSON格式的CommonResult并写入响应体common.WriteJSONResponse(w, http.StatusOK, common.DefaultSuccessMsg, person)
}
package entityimport nettype ClientInfo struct {Conn net.Conn
}
package entitytype Person struct {Name string json:nameAge int json:ageTitle string json:title
}
package httpServerimport (fmtnet/http
)// HTTPServer represents an HTTP tcpserver
type HTTPServer struct {routes map[string]http.HandlerFunc
}// NewHTTPServer creates a new HTTPServer instance
func NewHTTPServer() *HTTPServer {return HTTPServer{routes: make(map[string]http.HandlerFunc),}
}// AddRoute adds a new route to the HTTP tcpserver
func (s *HTTPServer) AddRoute(path string, handler http.HandlerFunc) {s.routes[path] handler
}// RunHTTPServer starts the HTTP tcpserver and sets up the routes
func (s *HTTPServer) RunHTTPServer() {for path, handler : range s.routes {// 添加http路由http.HandleFunc(path, handler)}err : http.ListenAndServe(:8080, nil)if err ! nil {fmt.Println(HTTP服务器启动失败:, err)}
}
package mainimport (testproject12/httpServertestproject12/routertestproject12/tcpserver
)func main() {// 创建并运行TCP服务器go tcpserver.NewServer().RunTCPServer()// 创建HTTP服务器httpserver : httpServer.NewHTTPServer()// 设置路由router.SetupRoutes(httpserver)// 启动HTTP服务器go httpserver.RunHTTPServer()select {}
}
package routerimport (testproject12/controllertestproject12/httpServer
)func SetupRoutes(server *httpServer.HTTPServer) {// 添加路由server.AddRoute(/person, controller.PersonHandler)// Add more routes as needed
}
package tcpserverimport (encoding/hexfmtnetsynctestproject12/entity
)// 所有用户信息
var AllConnectionList []net.Conn// Server 服务端结构体
type Server struct {Clients map[string]entity.ClientInfoMutex sync.Mutex
}// NewServer creates a new Server instance
func NewServer() *Server {return Server{Clients: make(map[string]entity.ClientInfo),}
}
func SendHexMessage(conn net.Conn, hexMessage string) error {messageBytes, err : hex.DecodeString(hexMessage)if err ! nil {fmt.Println(转换错误, err)return err}_, err conn.Write(messageBytes)if err ! nil {fmt.Println(发送消息失败, err)return err}return nil
}// RemoveConnection removes a connection from AllConnectionList
func RemoveConnection(conn net.Conn) {for i, c : range AllConnectionList {if c conn {AllConnectionList append(AllConnectionList[:i], AllConnectionList[i1:]...)break}}
}// RunTCPServer starts the TCP tcpserver
func (s *Server) RunTCPServer() {fmt.Println(服务端启动了)listen, err : net.Listen(tcp, 0.0.0.0:8888)if err ! nil {fmt.Println(监听失败, err)return}// 循环等待客户端的连接for {conn, err : listen.Accept()if err ! nil {fmt.Println(客户端的等待失败, err)return} else {fmt.Printf(等待连接成功:,con%v,接受到的客户端信息%v \n, conn, conn.RemoteAddr().String())// 服务端主动发送消息给客户端messageHex : 48656c6c6f2066726f6d20736572766572 // Hello from tcpserver 的十六进制表示SendHexMessage(conn, messageHex)}// 处理客户端连接的协程go s.handleClient(conn)}
}// 处理客户端连接
func (s *Server) handleClient(conn net.Conn) {defer conn.Close()// 获取客户端地址clientAddr : conn.RemoteAddr().String()// 将客户端信息存储到 map 中s.Mutex.Lock()s.Clients[clientAddr] entity.ClientInfo{Conn: conn, // 存储客户端连接对象}AllConnectionList append(AllConnectionList, conn)s.Mutex.Unlock()// 处理客户端消息for {buf : make([]byte, 1024)n, err : conn.Read(buf)if err ! nil {break}// 处理接收到的消息hexData : hex.EncodeToString(buf[:n])fmt.Println(收到客户端消息, clientAddr, :, hexData)}// 客户端断开连接从 map 中移除该用户s.Mutex.Lock()delete(s.Clients, clientAddr)RemoveConnection(conn)s.Mutex.Unlock()fmt.Println(客户端断开连接, clientAddr)
}
package utilimport (encoding/hextestproject12/tcpserver
)// SendHexMessage 发送十六进制消息// SendHexMessageToAllUser 广播发送十六进制消息
func SendHexMessageToAllUser(message string) {messageHex : hex.EncodeToString([]byte(message))for _, conn : range tcpserver.AllConnectionList {tcpserver.SendHexMessage(conn, messageHex)}
}
Dockerfile
# Use the official Golang image as the base image
FROM golang:1.21-alpine# Set the working directory inside the container
WORKDIR /go/src/app# Copy the contents of the current directory to the working directory inside the container
COPY . .# Build the Go application
RUN go build -o abc ./main# Add executable permission to the main binary
RUN chmod x ./abc# Expose the port the application runs on
EXPOSE 8888 8080# Define the command to run the application
CMD [./abc]
go.mod
module testproject12go 1.21.4