采用自定义协议代替OCX组件
事情起源:公司视频播放一直是采用的嵌入浏览器组件实现视频的预览回放等功能。这种实现方式要求客户使用ie浏览器。
最近上线项目使用html 5开发,要求ie11。项目中使用了视频播放功能,如果全部升级到ie11问题多,工作量大。
存在的主要问题:
有些系统开发较早,不能在ie11上运行。
部分客户电脑配置低还在使用xp。因为视频播放插件要求ie浏览器,所以支持h5的chrome,firefox等浏览器又不符合要求。
修改项目兼容低版本浏览器那是不可能的,只能修改视频播放插件。
1、视频插件改为 应用程序 安装到客户计算机,使用浏览器自定义协议启动。这样解决了浏览器兼容问题,几乎所有浏览器都支持自定义协议。
测试用例:注册表写入以下内容就可以启动 d:\myapp.exe
windows registry editor version 5.00
[hkey_classes_root\myapp]
@="url:autohotkey myapp protocol"
"url protocol"=""
[hkey_classes_root\myapp\defaulticon]
@="myapp.exe,1"
[hkey_classes_root\myapp\shell]
[hkey_classes_root\myapp\shell\open]
[hkey_classes_root\myapp\shell\open\command]
@="\"d:\\myapp.exe\" \"%1\""
javascript 点击启动myapp.exe
function launchapp() {
try {
window.location = 'myapp://,start';
}
catch (ex) {
errmsg = "启动 myapp 报错.\n\n";
alert(errmsg);
}
return;
};
2、如何判断用户有没安装视频应用呢??
网上搜索了一下,看着比较靠谱的:
https://www.cnblogs.com/tangjiao/p/9646855.html
这个实现方案是有缺陷,在chrome浏览器通过计时器和当前窗体失去焦点来判段。
计时器设置时间短路了,程序在配置低的电脑上会失败。
计时器设置时间长了,chrome上切换tab页就失去焦点,认为已经安装。
通过观察百度云盘的运行方式,得到启发。百度云盘也是采用了自定义协议。启动yundetectservice.exe 监听10000端口,
猜测web页面和yundetectservice.exe进行了通信。yundetectservice.exe启动成功会通知web页面。
3、采用 websocket 实现,视频应用启动的时候监听某端口等待 web页面连接,如果一定时间内连接成功就认为已经安装了插件。
web页面websocket连接的例子网上比较多。
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title>test</title> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> var nosupportmessage = "your browser cannot support websocket!"; var ws; function appendmessage(message) { $('body').append(message); } function connectsocketserver() { var support = "mozwebsocket" in window ? 'mozwebsocket' : ("websocket" in window ? 'websocket' : null); if (support == null) { appendmessage("* " + nosupportmessage + "<br/>"); return; } appendmessage("* connecting to server ..<br/>"); // create a new websocket and connect ws = new window[support]('ws://localhost:12345/'); // when data is comming from the server, this metod is called ws.onmessage = function (evt) { appendmessage("# " + evt.data + "<br />"); }; // when the connection is established, this method is called ws.onopen = function () { appendmessage('* connection open<br/>'); $('#messageinput').attr("disabled", ""); $('#sendbutton').attr("disabled", ""); $('#connectbutton').attr("disabled", "disabled"); $('#disconnectbutton').attr("disabled", ""); }; // when the connection is closed, this method is called ws.onclose = function () { appendmessage('* connection closed<br/>'); $('#messageinput').attr("disabled", "disabled"); $('#sendbutton').attr("disabled", "disabled"); $('#connectbutton').attr("disabled", ""); $('#disconnectbutton').attr("disabled", "disabled"); } } function sendmessage() { if (ws) { var messagebox = document.getelementbyid('messageinput'); ws.send(messagebox.value); messagebox.value = ""; } } function disconnectwebsocket() { if (ws) { ws.close(); } } function connectwebsocket() { connectsocketserver(); } window.onload = function () { $('#messageinput').attr("disabled", "disabled"); $('#sendbutton').attr("disabled", "disabled"); $('#disconnectbutton').attr("disabled", "disabled"); } </script> </head> <body> <input type="button" id="connectbutton" value="connect" onclick="connectwebsocket()"/> <input type="button" id="disconnectbutton" value="disconnect" onclick="disconnectwebsocket()"/> <input type="text" id="messageinput" /> <input type="button" id="sendbutton" value="send" onclick="sendmessage()"/> <br /> </body> </html>
实现websocket服务也有很多方法。
开源的有,superwebsocket,fleck,websocketlistener,websocket-sharp 等。
dotnet 高版本自带了system.net.websockets;
因为功能简单,仅仅是接受连接请求,所以采用dontnet自带的,不用引入多余的类库。
using system; using system.collections.generic; using system.linq; using system.net; using system.net.websockets; using system.text; using system.threading; using system.threading.tasks; namespace testserver { class program { static void main(string[] args) { runechoserver().wait(); } private static async task runechoserver() { httplistener listener = new httplistener(); listener.prefixes.add("http://localhost:12345/"); listener.start(); console.writeline("started"); while (true) { httplistenercontext context = listener.getcontext(); // if (!context.request.iswebsocketrequest) { context.response.close(); continue; } // console.writeline("accepted"); // var wscontext = await context.acceptwebsocketasync(null); var websocket = wscontext.websocket; // byte[] buffer = new byte[1024]; websocketreceiveresult received = await websocket.receiveasync(new arraysegment<byte>(buffer), cancellationtoken.none); while (received.messagetype != websocketmessagetype.close) { console.writeline($"echoing {received.count} bytes received in a {received.messagetype} message; fin={received.endofmessage}"); // echo anything we receive await websocket.sendasync(new arraysegment<byte>(buffer, 0, received.count), received.messagetype, received.endofmessage, cancellationtoken.none); received = await websocket.receiveasync(new arraysegment<byte>(buffer), cancellationtoken.none); } await websocket.closeasync(received.closestatus.value, received.closestatusdescription, cancellationtoken.none); websocket.dispose(); console.writeline("finished"); } } } }
至此,通过自定义协议和websocket 解决了视频插件的问题。
上一篇: C++中C风格字符串与字符数组实例讲解
下一篇: Python实现抢购IPhone手机