谷歌插件与桌面通讯

谷歌插件的运行是在一个封闭的沙盒中的,所以要想从本地读出相应的数据无法达到。这时,我们可以考虑SOCKET通信。

原理:本地运行一个SOCKET监听某个端口,插件向该服务端口发送信息,服务处理后,再返回相应的数据。

服务中使用的是SOCKET,实现代码如下

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Web;//需要引用System.Web程序集

namespace ChromeService
{
    public class ListenService
    {
        private TcpListener listener;
        private int listenPort = 20000;  
	private Encoding encoding_ = Encoding.UTF8;

        public ListenService()
        {
            try
            {              
                listener = new TcpListener(IPAddress.Parse("127.0.0.1"), listenPort);
                listener.Start();           
                Thread listenThread = new Thread(StartListen);
                listenThread.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine("ListenService Exception :" + ex.Message);
            }
        }

        public void StartListen()
        {

            while (true)
            {
                try
                {
                    Socket newSocket = listener.AcceptSocket();
                    if (!newSocket.Connected)
                    {
                        continue;
                    }

                    Byte[] receive = new Byte[10240];
                    newSocket.Receive(receive, receive.Length, 0);
                    byte[] backData = handle(receive);
                    newSocket.Send(backData, backData.Length, 0);
                    newSocket.Close();
                    continue;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

  	public byte[] handle(byte[] cmdPack)
        {
            string bufferStr = encoding_.GetString(cmdPack);
            byte[] backData = new byte[2] { 0, 0 };

            //只处理"get"请求类型
            if (bufferStr.Substring(0, 3) != "GET")
            {
                return backData;
            }

            int dataBegin = bufferStr.IndexOf(@"/") + @"/".Length;
            int dataEnd = bufferStr.IndexOf("HTTP");
            string cmdPackStr = bufferStr.Substring(dataBegin, dataEnd - dataBegin);
            string cmdData = "";
            if (cmdPackStr.Length > 2)
            {
                cmdData = cmdPackStr.Substring(2);
                cmdData = HttpUtility.UrlDecode(cmdData);//如果插件发送的数据有中文时,需要进行解码
            }
            string cmd = cmdPackStr.Substring(0, 2).Trim();//从插件发送来的命令占两个字符00-FF
            switch (cmd)
            {
                case "0F":
                    {                        
                        backData = encoding_.GetBytes("OK");
                        break;
                    }
                default:
                    {
                        backData = encoding_.GetBytes("DEFAULT");
                        break;
                    }
            }

            return backData;
        }

    }
}

在插件中使用的是XHR或者JQUERY的AJAX,实现代码如下

/// <reference path="../libs/jquery-1.7.2.min.js"/>
var Communicate = {};

Communicate.send = function (cmd, data, callback) {//jquery的ajax实现
    var sendCmdData = "";
    if (cmd === undefined || cmd === null) {
        return;
    }
    sendCmdData += cmd;
    if (data !== undefined || data !== null) {
        sendCmdData += data;
    }
    var url = 'http://localhost:20000/' + sendCmdData
    $.ajax({ url: url, async: false, success: callback });
}
Communicate.send = function (cmd, data, callback) {//xhr实现
    var xhr = new XMLHttpRequest();
    var sendCmdData = "";
    if (cmd === undefined || cmd === null) {
        return;
    }
    sendCmdData += cmd;
    if (data !== undefined || data !== null) {
        sendCmdData += data;
    }
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) //200:Success.304:Tell browser to read cache.
            {
                //alert(xhr.responseText);
                if (callback === undefined || callback === null) {
                    return;
                }
                callback(xhr.responseText);
            }
        }
    }

    xhr.open("GET", 'http://localhost:20000/' + sendCmdData, true);
    xhr.send(null);
}

转载请注明出处 http://blog.csdn.net/xxdddail/article/details/13507379

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值