我正在尝试在mvc5应用程序中使用node.js和websocket实现聊天功能,为此我正在使用URL重写器。
我使用以下代码创建了一个node服务器。
var app = require('express')();
//creating http server
var server = require('http').createServer(app);
//add webrtc.io functionality to http server
var webRTC = require('webrtc.io').listen(server);
//port which is allocated dynamically by visual studeo IIS/iisexpress server, which of string formate.
var port = process.env.PORT;
//let the server in listen mode for the port id assigned by IIS server.
server.listen(port);
//this is for testing purpose, which returns the string, for the specified url request
app.get('/test/websocketcon', function (req, res)
{
res.end("working");
});
如果我尝试访问https://localhost:44300/test/websocketcon,我会得到“working”的响应。但是,如果我尝试创建新的websocket,就会收到错误消息:
WebSocket连接到'wss://localhost:44300/Home/websocketcon'失败:在WebSocket握手期间发生错误:意外的响应代码:404
我尝试的创建新websocket的代码:
var protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://';
var address = protocol + window.location.host + window.location.pathname + "/websocketcon";
var createdwebsocket = new WebSocket(address );