收藏
回答

微信小程序连接后台websocket总是失败,以前是可以的,是什么原因吗,怎么解决?

错误提示为:webSocket connection to wss failed:(env windows,mp,1.06.2210310;lib:2.25.0)

微信前端程序:

wx.connectSocket({

        url: 'wss://52954z94s9.imdo.co',

        //url: 'ws://61.183.215.162:9082',

        header:{

          'content-type': 'application/json'

        },

        success: () => {

          console.log('connectSocket 连接成功')

        },

      })

    }

后台代码用c#所写:

 WebSocketServer server = new WebSocketServer("ws://127.0.0.1:9082/ws");

 server.RestartAfterListenError = true;

            server.Start(socket =>

            {

                socket.OnOpen = () =>

                {

                    string clientUrl = socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort;

                    dic_Sockets.Add(clientUrl, socket);

                    Console.WriteLine(DateTime.Now.ToString() + "|服务器:和客户端⽹页:" + clientUrl + " 建⽴WebSock连接!");

                };

                socket.OnClose = () =>

                {

                    string clientUrl = socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort;

                    if (dic_Sockets.ContainsKey(clientUrl))

                    {

                        dic_Sockets.Remove(clientUrl);

                    }

                    Console.WriteLine(DateTime.Now.ToString() + "|服务器:和客户端⽹页:" + clientUrl + " 断开WebSock连接!");

                };

回答关注问题邀请回答
收藏
登录 后发表内容