WebSocket连接到'wss://localhost:44300/Home/websocketcon'
WebSocket connection to 'wss://localhost:44300/Home/websocketcon' failed: Error during WebSocket handshake: Unexpected response code: 404
我正在尝试使用node.js和websocket在mvc5应用程序中实现websocket聊天,为此我正在使用URL重写器。
我创建了一个节点服务器,代码如下。
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.我得到的回应是"工作"。但是,如果我试图创建新的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 );
您的快速路由/服务器侦听http请求,而不是wss。看看这个:https://www.npmjs.com/package/express-ws
深入解释:
使用以下代码行,您创建了一个http服务器:
var app = require('express')();
var server = require('http').createServer(app);
http是连接到http://yoursite.com
时使用的协议。但是,您正在尝试将网络套接字连接到服务器。要做到这一点,您需要添加一个websocket侦听器并路由到您的服务器。这是因为websocket不通过http协议工作,而是通过websocket协议工作。
要制作一个websocket服务器,请检查我上面提供的链接/模块。您应该有一个同时侦听http请求和websocket请求的服务器。要使您当前的代码与websocket一起工作,您需要做的是进行以下更改:
var app = require('express')();
var server = require('http').createServer(app);
// now add the express websocket functionality
var expressWs = require('express-ws')(app);
.
.
.
app.ws('/test/websocketcon', function (ws, req)
{
ws.send("Working!");
ws.on('message', function(msg) {
ws.send(msg);
});
});
相关文章:
- `npm install` killed for large package.json, no npm-debug.lo
- $('#element').css("CssProperty") is not lo
- 在使用 Lo-Dash 3.0 转换值时分组的任何好方法
- 路径 lo 加载本地主机子文件夹中的文件不起作用.js仅在基路径中
- 在不修改基础对象的情况下使用 Lo-Dash 合并
- 为什么全栈角度生成器使用 Lo-Dash 的合并而不是 document.set
- JavaScript/jQuery LO 中的最佳方法 从 XML 数据填充 oblects
- 是懒惰的.js像Lo-Dash这样的下划线的直接替代品
- 如何使用Lo Dash扩展对象
- 使用下划线/Lo短划线更新集合对象
- WebSocket连接到'wss://localhost:44300/Home/websocketcon'
- 如何使用Lo-dash在_.filter的回调函数中迭代数组
- Lo Dash-帮我理解为什么_.pick没有'I don’我没有按我期望的方式工作
- 当使用BackboneJS时,我应该使用Lo-Dash的下划线兼容构建吗?
- Javascript lo-dash
- Inheritance with Lo-Dash
- 为什么Lo-Dash _.每一个都比Boiler.js _快.和Underscore.js .each
- window.location.href vs window.location.replace vs window.lo
- Lo-Dash,数组和集合的区别
- 在RequireJS环境中同时使用Lo-Dash和Underscore