关灯
收起左侧

[网络技术] Tencent://Message/协议的实现原理 URL协议注册

0
回复
523
查看
[复制链接]

 成长值: 12773

个人资料

职务: 董事会

铜板: 1477 个

金币: 1001 个

荣耀勋章

超级版主开国元勋夜猫子勋章春节勋章论坛一周年最佳新人活跃会员志愿者推广达人宣传达人灌水之王突出贡献版主荣誉管理论坛元勋

发表于 2019-10-4 16:35:18 | 显示全部楼层 |阅读模式

 
腾讯官方通过 Tencent://Message/协议可以让QQ用户显示QQ/TM的在线状态发布在互联网上;并且点击 ,不用加好友也可以聊天:
具体代码:
  1. <a href="tencent://message/?uin=215555521&Site=JooIT.com&Menu=yes">
  2. <img border="0" SRC='http://wpa.qq.com/pa?p=1:215555521:3' alt="点击这里给我发消息">
  3. </a>
复制代码
但它是如何实现的呢?下面文章以及微软官方说明详细解释了其工作原理:
Register protocol,此文中对于 Windows、Linux 和 OS X 操作系统如何注册协议都有说明。比如说 Windows,其实只需写入注册表,即可实现协议与执行程序的关联。例如腾讯的Tencent://Message协议注册表如下:
  1. [HKEY_CLASSES_ROOT"TENCENT]
  2. @="TencentProtocol"
  3. “URL Protocol"="D:""Program Files""Tencent""QQ""Timwp.exe"

  4. [HKEY_CLASSES_ROOT"TENCENT"DefaultIcon]
  5. @="D:""Program Files""Tencent""QQ""Timwp.exe,1"

  6. [HKEY_CLASSES_ROOT"TENCENT"shell]

  7. [HKEY_CLASSES_ROOT"TENCENT"shell"open]

  8. [HKEY_CLASSES_ROOT"TENCENT"shell"open"command]
  9. @="""D:""Program Files""Tencent""QQ""Timwp.exe"" ""%1"""
复制代码
此注册表所实现的就是当浏览器(或其它)碰到 tencent://… 时,自动调用 Timwp.exe,并把 tencent://… 地址作为第一个参数传递给 Timwp.exe。
程序代码:
  1. [HKEY_CLASSES_ROOT"TENCENT]
  2. @="TencentProtocol"
  3. “URL Protocol"="D:""Program Files""Tencent""QQ""Timwp.exe"

  4. [HKEY_CLASSES_ROOT"TENCENT"DefaultIcon]
  5. @="D:""Program Files""Tencent""QQ""Timwp.exe,1"

  6. [HKEY_CLASSES_ROOT"TENCENT"shell]

  7. [HKEY_CLASSES_ROOT"TENCENT"shell"open]

  8. [HKEY_CLASSES_ROOT"TENCENT"shell"open"command]
  9. @="""D:""Program Files""Tencent""QQ""Timwp.exe"" ""%1"""

复制代码
  1. 此注册表所实现的就是当浏览器(或其它)碰到 tencent://… 时,自动调用 Timwp.exe,并把 tencent://… 地址作为第一个参数传递给 Timwp.exe。
复制代码
  1. 程序代码:

  2. A protocol is a method that is used to send, receive, and handle information over a connection. Common protocols viewed from the browser include http, ftp, and mailto. In order for you to view information sent over a specific protocol, it must be registered. Once registered, the protocol can then be handled by the program you specify, such as your browser or a 3rd party viewer. This means that a hyperlink ( e.g. foo://fred ) can use the handler for protocol foo to open the file named fred.

  3. Contents [hide]
  4. 1 Registering an unsupported protocol
  5. 1.1 Windows
  6. 1.2 Linux
  7. 1.3 OS X
  8. 2 Redirecting a registered protocol



  9. [edit]Registering an unsupported protocol
  10. Mozilla products utilize protocols defined internally, as well as those defined by the operating system. You can add the ability to use an unsupported protocol by registering it. The OS-specific method of doing this is described below.

  11. [edit]Windows
  12. Create the registry .reg file, replacing foo with your unregistered protocol, and the path with whatever handler program you want to run. Then merge it into the Windows registry.


  13. REGEDIT4

  14. [HKEY_CLASSES_ROOT"foo]
  15. @="URL:foo Protocol"
  16. "URL Protocol"=""

  17. [HKEY_CLASSES_ROOT"foo"shell]

  18. [HKEY_CLASSES_ROOT"foo"shell"open]

  19. [HKEY_CLASSES_ROOT"foo"shell"open"command]
  20. @="""C:""Program Files""Application""program.exe"" ""%1"""
  21. See Registering an Application to a URL Protocol for additional information.

  22. [edit]Linux
  23. Registration is unnecessary. Simply associate whatever proto: with a program through Firefox: Example: Add the sip: protocol to launch kphone for VoIP calls in Firefox:

  24. - Type about:config into the address bar
  25. - Right-click create new boolean value: network.protocol-handler.external.sip and set to true
  26. - Right-click create new boolean value: network.protocol-handler.warn-external.sip and set to false
  27. - Right-click create new string value: network.protocol-handler.app.sip and set to /usr/bin/kphone
  28. This will actually launch kphone. Not sure if it will dial though. That is untested :)

  29. You can also optionally register the protocol with whatever window manager you are using. In KDE this is done through Control Center - KDE Components - File Associations. This step is usually unnecessary unless your window manager has a custom browser, such as konqueror.

  30. [edit]OS X
  31. Probably very similar to Linux (above).

  32. [edit]Redirecting a registered protocol
  33. If the protocol is already handled by the browser, you can specify what program will be used as a handler to open the file. To do this, add the pref:
  34. network.protocol-handler.app.foo as a string with value C:"Program Files"Application"program.exe
  35. Note: If the path or name is incorrect, the browser will display an error saying "protocol (foo) isn't associated with any program". (See bug 312953).

  36. You may also need to use the following prefs, although this is uncertain:
  37. network.protocol-handler.external.foo = true
  38. network.protocol-handler.expose.foo = false
复制代码

回复

使用道具 举报

 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

官方微信

APP下载

24小时客服QQ

3300-1700-74
Copyright ©  2018-2020   天马论坛 (吉ICP备19000744号-1