由于项目要进行H.264裸流播放,在github上找到了可用的
wfs.js,最近又遇到了一个问题,无法调用wfs.js内部Websocket初始化和关闭的函数。
之前经过提问已经解决了
调用函数内部函数的问题,答案中的链接地址是https://blog.csdn.net/weixin_43694639/article/details/88723280,
但是我用同样的写法,却不能调用websocket的关闭函数,只能调用发送数据的函数。
这是整个函数折叠起来的样子:
(function(f)){...
})(function() {...
});
这是我要调用的函数整体:
var WebsocketLoader = function(_EventHandler){
_inherits(WebsocketLoader, _EventHandler);
function WebsocketLoader(wfs){...
}
_createClass(WebsocketLoader, [{... //要调用的函数在省略号里
}]);
return WebsocketLoader;
}(_eventHandler2.default);
exports.default = WebsocketLoader;
不能调用的函数,初始化websocket:
key: 'initSocketClient',
value: function initSocketClient() {
this.client.binaryType = 'arraybuffer';
this.client.onmessage = this.receiveSocketMessage.bind(this);
// clientSocket.binaryType = 'arraybuffer';
// clientSocket.onmessage = socketReceive;
this.wfs.trigger(_events2.default.WEBSOCKET_MESSAGE_SENDING, { commandType: "open", channelName: this.channelName, commandValue: "NA" }); //不知道这个trigger是什么
console.log('Websocket Open!');
flagSP = true;
//initSocket = initSocketClient
不能调用的函数,主动关闭websocket:
//这个函数是我后来自己加的
key: 'onWebsocketClose',
value: function onWebsocketClose(i) {
clientSocket.send(i)
console.log('切换页面,中断连接。' + i)
clientSocket.close();
socketClose = onWebsocketClose;
每次调用都会报这个错:
[Vue warn]: Error in v-on handler: "TypeError: Object(...) is not a function"
found in
---> <WindowFrame> at src/components/WindowFrame.vue
<ElHeader> at packages/header/src/main.vue
<ElContainer> at packages/container/src/main.vue
<Ccqg> at src/components/main.vue
<App> at src/App.vue
<Root> vue.esm.js:628
TypeError: "Object(...) is not a function"
showMain WindowFrame.vue:309
click WindowFrame.vue:268
VueJS 3
但是这个函数可以在外部调用:
key: 'onWebsocketMessageSending',
value: function onWebsocketMessageSending(i) {
clientSocket.send(i)
console.log('发送视频请求:' + i)
//this.client.send(i)
//this.client.send(JSON.stringify({ type: 2, carNum: 8888 }))
sendMsg = onWebsocketMessageSending
我之前写过单独的websocket,可以直接在外部调用我定义的所有方法,包括初始化和关闭。不明白为什么这里不行。
https://blog.csdn.net/weixin_43694639/article/details/88723280
应该是外部调用wfsjs的Wfs类中的attachMedia函数获取参数,然后wfsjs内部去调用的WebsocketLoader这个类中的initSocketClient去初始化服务,这是我理解的初始化服务,请问你是怎么关闭服务,我调用了Wfs类中的destroy函数没有报错,但是也没有关闭
肯定是可以的。我在Wfs.js上增加了一个单独发送消息的方法。开始也是不行,包括Destroy也不生效,分析发现不是函数不生效,而是WebSocket服务器端的机制有问题,服务器端一旦收到客户端的连接请求之后,建立连接,就不停地Send H264帧,而没有线程进行Receive监听,所以接收到客户端发出的消息(包括Destroy方法发出的拆连接请求)都没有发送响应,所以连接一直挂着断不掉。好好检查一下服务器端的代码吧。我在原来的WebsocketLoader中增加了一个判断是否已连接的逻辑和一个发送消息的方法 :
var WebsocketLoader = function (_EventHandler) {
_inherits(WebsocketLoader, _EventHandler);
function WebsocketLoader(wfs) {
_classCallCheck(this, WebsocketLoader);
var _this = _possibleConstructorReturn(this, (WebsocketLoader.__proto__ || Object.getPrototypeOf(WebsocketLoader)).call(this, wfs, _events2.default.WEBSOCKET_ATTACHING, _events2.default.WEBSOCKET_DATA_UPLOADING, _events2.default.WEBSOCKET_MESSAGE_SENDING));
_this.buf = null;
_this.slicesReader = new _h264NalSlicesreader2.default(wfs);
_this.mediaType = undefined;
_this.channelName = undefined;
_this.camId = undefined;
return _this;
}
_createClass(WebsocketLoader, [{
key: 'destroy',
value: function destroy() {
console.log("Force closing websocket connection.");
!!this.client && this.client.send("stopH264Streaming");
!!this.client && this.client.close();
this.slicesReader.destroy();
_eventHandler2.default.prototype.destroy.call(this);
}
}, {
key: 'isConnected',
value: function isConnected() {
return !!this.client;
}
}, {
key: 'sendMsg',
value: function sendMsg(msgText) {
console.log("Sending msg: "+msgText);
!!this.client && this.client.send(msgText);
}
}, {
key: 'onWebsocketAttaching',
value: function onWebsocketAttaching(data) {
this.mediaType = data.mediaType;
this.channelName = data.channelName;
this.camId = data.camId;
if (data.websocket instanceof WebSocket) {
this.client = data.websocket;
this.client.onopen = this.initSocketClient.bind(this);
this.client.onclose = function (e) {
console.log('Websocket Disconnected!');
};
}
}
}, {
key: 'initSocketClient',
value: function initSocketClient(client) {
this.client.binaryType = 'arraybuffer';
this.client.send("admin:Huawei@123");
this.client.onmessage = this.receiveSocketMessage.bind(this);
this.wfs.trigger(_events2.default.WEBSOCKET_MESSAGE_SENDING, { commandType: "open", channelName: this.channelName, camId: this.camId, commandValue: "NA" });
this.client.send("start");
console.log('Websocket Open!');
}
}, {
key: 'receiveSocketMessage',
value: function receiveSocketMessage(event) {
this.buf = new Uint8Array(event.data);
var copy = new Uint8Array(this.buf);
this.client.send("ok");
if (this.mediaType === 'FMp4') {
this.wfs.trigger(_events2.default.WEBSOCKET_ATTACHED, { payload: copy });
}
if (this.mediaType === 'H264Raw') {
this.wfs.trigger(_events2.default.H264_DATA_PARSING, { data: copy });
}
}
}, {
key: 'onWebsocketDataUploading',
value: function onWebsocketDataUploading(event) {
this.client.send(event.data);
}
}, {
key: 'onWebsocketMessageSending',
value: function onWebsocketMessageSending(event) {
this.client.send(JSON.stringify({ t: event.commandType, c: event.channelName, camId: event.camId, v: event.commandValue }));
}
}]);
return WebsocketLoader;
}(_eventHandler2.default);