我很难制作WSS Xsockets。我在IIS7中制作了一个自签名证书。我尝试通过JS多次连接,但根本不会。令人费解的是,非安全实施工作绝对正常。
我的服务器代码如下:
public class SSLConfig : ConfigurationSetting
{
public SSLConfig()
: base("wss://localhost:4509")
{
this.CertificateLocation = System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine;
this.CertificateSubjectDistinguishedName = "cn=localhost";
}
}
class Program
{
static void Main(string[] args)
{
var myCustomConfigs = new List<IConfigurationSetting>();
myCustomConfigs.Add(new SSLConfig());
using (var server = Composable.GetExport<IXSocketServerContainer>())
{
Console.WriteLine("running");
server.StartServers(configurationSettings: myCustomConfigs);
foreach (var serv in server.Servers)
{
Console.WriteLine(serv.ConfigurationSetting.Endpoint);
}
Console.ReadLine();
server.StopServers();
}
}
}
public class TestSockets : XSocketController
{
public TestSockets()
{
}
public void Echo(string message)
{
this.Send(new
{
Message = message
}.AsTextArgs("message"));
}
}
使用Javascript:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="\scripts\XSockets.latest.js"></script>
<script>
var ws;
$(function () {
ws = new XSockets.WebSocket("wss://localhost:4509/TestSockets");
ws.on(XSockets.Events.open, function (clientInfo) {
alert(clientInfo);
console.log('Open', clientInfo);
});
ws.on(XSockets.Events.onError, function (err) {
alert(err);
console.log('Error', err);
});
$("#btnSocket").on("click", function () {
ws.publish("Echo", { message: XSockets.Utils.randomString(50) });
});
});
</script>
</head>
<body>
<input type="button" value="click" id="btnSocket"/>
</body>
</html>
答案 0 :(得分:0)
事实证明,由于使用了自签名证书,Firefox在尝试建立连接时抛出了错误(Chrome只是保持沉默......)。
使用https://而不是wss://访问URI后,我能够在所有浏览器上添加例外,现在我可以连接到wss服务器。