在腾讯云上买的服务器server2008,ssl证书安装完毕,域名绑定成功,端口开通了。使用ws协议已经可以让小程序链接服务器并和服务器交互信息。但是在发布前想改用wss协议发现wx.connectionsocket()出现问题。(服务器已经添加了绑定证书的相应代码)
- 当前 Bug 的表现(可附上截图)
小程序:
服务器(使用c#编写的控制台程序):
- 预期表现
小程序会将用户名和密码以“0用户名 密码”的字符串样式发送到服务器,服务器接收到后向小程序发送“correct”
- 复现路径
- 提供一个最简复现 Demo
小程序代码:
Page({
/**
* 页面的初始数据
*/
data: {
phone: ‘’,
password: ‘’,
socketOpen : false,
//socketMsgQueue : [],
},
/**
* 生命周期函数–监听页面加载
*/
onLoad: function (options) {
//页面初始化 options为页面跳转所带来的参数
var that = this;
wx.onSocketMessage(function (res) {
console.log(‘收到服务器内容:’ + res.data)
that.setData({ message: res.data })
wx.redirectTo({
url: ‘…/frame/frame1’
})
})
wx.onSocketClose(function (res) {
console.log(‘WebSocket 已关闭!’)
})
},
/**
* 生命周期函数–监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数–监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数–监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数–监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数–监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
// 获取输入账号
phoneInput: function (e) {
this.setData({
phone: e.detail.value
})
},
// 获取输入密码
passwordInput: function (e) {
this.setData({
password: e.detail.value
})
},
// 登录
login: function () {
var that = this;
if (that.data.phone.length == 0 || that.data.password.length == 0) {
wx.showToast({
title: ‘用户名和密码不能为空’,
icon: ‘loading’,
duration: 2000
})
} else {
// 这里修改成跳转的页面
wx.connectSocket({
//url:‘wss://172.16.4.98:40001’,
//url: ‘ws://140.143.53.231:9000’,
url: ‘wss://stserver.xyz:30000’,
/*header: {
‘Sec-WebSocket-Protocol’: this._protocols //need add this
},
protocols: [‘protocol1’],*/
method: “GET”
})
wx.onSocketError(function (res) {
console.log(‘WebSocket连接打开失败,请检查!’+res.data)
})
wx.onSocketOpen(function (res) {
that.setData({
socketOpen:true
})
sendSocketMessage(‘0’+that.data.phone + ’ ’ + that.data.password)
})
function sendSocketMessage(msg) {
if (that.data.socketOpen) {
wx.sendSocketMessage({
data: msg,
success: (res) => {
//console.log(res.data)
console.log(‘收到服务器内容:’ + res.data)
},
fail: (res) => {
console.log(‘失败’);
}
})
} else {
socketMsgQueue.push(msg)
}
}
if (that.data.socketOpen) {
wx.showToast({
title: ‘登录成功’,
icon: ‘success’,
duration: 2000
})
} else {
wx.showToast({
title: ‘登录失败’,
icon: ‘fault’,
duration: 2000
})
}
}
}
})
服务器代码:
//客户端url以及其对应的Socket对象字典
IDictionary<string, IWebSocketConnection> dic_Sockets = new Dictionary<string, IWebSocketConnection>();
//创建
WebSocketServer server = new WebSocketServer(“wss://0.0.0.0:30000”);//监听所有的的地址
//出错后进行重启
//开启监听函数
public startListener()
{
server.Certificate = new X509Certificate2(@“C:\IIS\stserver.xyz.pfx”, “gbj10ywxif41v9”);
//server.EnabledSslProtocols = SslProtocols.Ssl3 | SslProtocols.Tls;
server.RestartAfterListenError = true;
//WebSocketBehavior//类的IgnoreExtensions
server.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Ssl3;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
//定义变量链接地址
string clientUrl;
//开始监听
server.Start(socket =>
{
socket.OnOpen = () => //连接建立事件
{
//获取客户端网页的url
clientUrl = socket.ConnectionInfo.ClientIpAddress + “:” + socket.ConnectionInfo.ClientPort;
dic_Sockets.Add(clientUrl, socket);
Console.WriteLine(DateTime.Now.ToString() + “|服务器:和客户端网页:” + clientUrl + " 建立WebSock连接!");
socket.OnMessage = message => //接受客户端网页消息事件
{
clientUrl = socket.ConnectionInfo.ClientIpAddress + “:” + socket.ConnectionInfo.ClientPort;
Console.WriteLine(DateTime.Now.ToString() + “|服务器:【收到】来客户端网页:” + clientUrl + “的信息:\n” + message);
if (message.Substring(0, 1) == “0”)
{
for (int i = 0; i < message.Length; i++)
{
if (message.Substring(i, 1) == " ")
{
bool b = verifyLoginInformation(message.Substring(1), i-1);
if (b == true)
{
socket.Send(“correct”);
}
}
}
}
else
{
foreach (var item in dic_Sockets.Values)
{
if (item.IsAvailable == true)
{
item.Send(“有人发消息:” + message.Substring(1));
}
}
}
};
};
socket.OnClose = () => //连接关闭事件
{
clientUrl = socket.ConnectionInfo.ClientIpAddress + “:” + socket.ConnectionInfo.ClientPort;
//如果存在这个客户端,那么对这个socket进行移除
if (dic_Sockets.ContainsKey(clientUrl))
{
//注:Fleck中有释放
//关闭对象连接
if (dic_Sockets[clientUrl] != null)
{
dic_Sockets[clientUrl].Close();
}
dic_Sockets.Remove(clientUrl);
}
Console.WriteLine(DateTime.Now.ToString() + “|服务器:和客户端网页:” + clientUrl + " 断开WebSock连接!");
};
});
}