问题内容
我有一个很大程度上基于本教程的服务器。在我对其应用了额外的更改后,它工作得很好。但现在我尝试向其中添加 socket.io ,它现在遇到了一些问题。在测试我添加的 socket.io 代码后,它似乎影响了客户端代码(端点)调用服务器端代码(数据库查询、处理)的机制。当端点调用的日志出现在终端上时,调用到达服务器的客户端,但它似乎并未调用服务器端。
这是套接字服务器的代码:
package helpersimport ( "fmt" "net/http" socketio "github.com/googollee/go-socket.io" "github.com/googollee/go-socket.io/engineio" "github.com/googollee/go-socket.io/engineio/transport" "github.com/googollee/go-socket.io/engineio/transport/polling" "github.com/googollee/go-socket.io/engineio/transport/websocket")var alloworiginfunc = func(r *http.request) bool { return true}func startsocket() { server := socketio.newserver(&engineio.options{ transports: []transport.transport{ &polling.transport{ checkorigin: alloworiginfunc, }, &websocket.transport{ checkorigin: alloworiginfunc, }, }, }) server.onconnect("/", func(s socketio.conn) error { s.setcontext("") fmt.println("connected:", s.id()) return nil }) server.onevent("/", "notice", func(s socketio.conn, msg string) { fmt.println("notice:", msg) s.emit("reply", "have "+msg) }) server.onerror("/", func(s socketio.conn, e error) { fmt.println("socket error:", e) }) server.ondisconnect("/", func(s socketio.conn, reason string) { fmt.println("closed", reason) }) go server.serve() defer server.close() http.handle("/socket.io/", server) http.handle("/", http.fileserver(http.dir("./asset"))) fmt.println("socket server serving at localhost:8000...") fmt.print(http.listenandserve(":8000", nil))}
登录后复制
// main.go 服务器端
package mainimport ( "flag" "fmt" "log" "net" pb "github.com///api/proto/out" "github.com///cmd/server/handlers" "github.com///cmd/server/helpers" "google.golang.org/grpc" "google.golang.org/grpc/credentials")func init() { helpers.databaseconnection()}var ( tls = flag.bool("tls", true, "connection uses tls if true, else plain tcp") certfile = flag.string("cert_file", "", "the tls cert file") keyfile = flag.string("key_file", "", "the tls key file") port = flag.int("port", 50051, "the server port"))func main() { flag.parse() // helpers.startsocket() lis, err := net.listen("tcp", fmt.sprintf("localhost:%d", *port)) if err != nil { log.fatalf("failed to listen: %v", err) } var opts []grpc.serveroption if *tls { if *certfile == "" { *certfile = "service.pem" } if *keyfile == "" { *keyfile = "service.key" } creds, err := credentials.newservertlsfromfile(*certfile, *keyfile) if err != nil { log.fatalf("failed to generate credentials: %v", err) } opts = []grpc.serveroption{grpc.creds(creds)} } mserv := grpc.newserver(opts...) fmt.println("grpc server running ...") pb.registersomethingserviceserver(mserv, &handlers.somethingserver{}) log.printf("server listening at %v", lis.addr()) if err := mserv.serve(lis); err != nil { log.fatalf("failed to serve : %v", err) }}
登录后复制
// main.go 客户端
立即学习“go语言免费学习笔记(深入)”;
package mainimport ( "bytes" "encoding/json" "flag" "fmt" "log" "sort" "strings" "github.com///cmd/client/handlers" "github.com/gin-gonic/gin" "google.golang.org/grpc" "google.golang.org/grpc/credentials")var ( addr = flag.string("addr", "localhost:50051", "the address to connect to"))func main() { flag.parse() creds, err := credentials.newclienttlsfromfile("service.pem", "") if err != nil { log.fatalf("could not process the credentials: %v", err) } conn, err := grpc.dial(*addr, grpc.withtransportcredentials(creds)) if err != nil { log.fatalf("did not connect: %v", err) } defer conn.close() var gingine = gin.default() startclient(conn, gingine)}func startclient(conn *grpc.clientconn, gingine *gin.engine) { handlers.somethingapihandler(conn, gingine) gingine.run(":5000")}
登录后复制
有关完整的客户端和服务器代码,您可以查看我上面链接的教程。如果我不调用 startsocket(),一切正常。
当调用 api 的端点时,我收到此错误,该错误是由调用服务器端代码的代码引发的:
"rpc error: code = unavailable desc = connection error: desc ="transport: error while dialing: dial tcp [::1]:50051: connectex: noconnection could be made because the target machine actively refusedit.""
登录后复制
代码如下:
ginGine.POST("/account/login", func(ctx *gin.Context) { var account models.Account err := ctx.ShouldBind(&account) if err != nil { ctx.JSON(http.StatusBadRequest, gin.H{ "error1": err.Error(), }) return } res, err := srvClient.service.ValidateAccount(ctx, &pb.ValidateAccountRequest{ Account: &pb.Account{ Id: account.ID, FirstName: account.FirstName, LastName: account.LastName, UserName: account.UserName, Password: account.Password, }, }) if err != nil { ctx.JSON(http.StatusBadRequest, gin.H{ "error2": err.Error(), }) return } ctx.JSON(http.StatusOK, gin.H{ "status": res.Status, "access_token": res.AccessToken, })})
登录后复制
error2 是 api 调用返回的内容
正确答案
这行代码是阻塞的,函数 StartSocket() 永远不会返回:
fmt.Print(http.ListenAndServe(“:8000”, nil))
为了测试,您可以在其后面添加日志,并且该日志消息将不会打印。
您需要在单独的非阻塞 goroutine 中运行 StartSocket()。
附注例如,该行是 log.Fatal(http.ListenAndServe(“:8000”, nil))。您的代码吞下了一个错误。
以上就是启动套接字服务器会干扰 gRPC/http 客户端服务器通信 Golang的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2485672.html