前台 WebSocket配置,通过URL协议判断后台连接地址及协议
var wsUri = location.protocol === 'https:' ? 'wss://www.*.com:9252' : 'ws://192.168.1.111:9253';
websocket = new WebSocket(wsUri);
websocket.onopen = function (evt) {
websocket.send("login:" + currentUserID + ":" + sessionID);
};
后端 关键代码
public WebSocketServer WebSocketServer;
WebSocketServer = new WebSocketServer();
ServerConfig serverConfig = new ServerConfig();
serverConfig.Port = WSPort;//配置文件读取
serverConfig.MaxConnectionNumber = WSMaxConnectionNumber;//配置文件读取
if (openWSS)//配置文件读取
{
//开启SSL wss://
serverConfig.Security = "tls";//FilePath证书的绝对路径配置文件读取,Password证书密码
CertificateConfig objCertificateConfig = new CertificateConfig() { FilePath = certPath, Password = CertPassword };
serverConfig.Certificate = objCertificateConfig;
}WebSocketServer.Setup(serverConfig)
经过测试正常开启ws和wss无法在后台服务器共存,因为为启动程序之初就已定义了是否开启,因目前项目对此要求不高暂使用两个后台服务进行处理,如果有需要一个服务解决问题的朋友请参考如下文章.
WebSocket和SSL加密的WebSocket(即同时支持ws和wss):"https://www.cnblogs.com/zhuweisky/p/7850297.html
WebSocket学习:https://blog.csdn.net/qq_35955916/article/details/86529647