connect.io
Advanced tools
Comparing version 0.4.1 to 0.5.0
@@ -11,7 +11,23 @@ import Port from './port'; | ||
* @param {String|Number} [eIdOrTabId] - 扩展 id 或标签页 id。默认值为 chrome.runtime.id | ||
* @param {Number} [frameId] - 如果第一个参数是 tabId,则此参数指定要连接到指定标签页的某个 frame。默认会连接到指定标签页的所有 frame。 | ||
* @param {Object} [options] - 设置 | ||
* @param {Number} [options.frameId] - 如果第一个参数是 tabId,则此参数指定要连接到指定标签页的某个 frame。默认会连接到指定标签页的所有 frame。 | ||
* @param {String} [options.namespace] - 此客户端所在的命名空间的名称。默认为 default | ||
* @see https://developer.chrome.com/extensions/runtime#method-connect | ||
* @see https://developer.chrome.com/extensions/tabs#method-connect | ||
*/ | ||
constructor( eIdOrTabId = id , frameId ) { | ||
constructor( eIdOrTabId = id , options ) { | ||
// new Client(options) | ||
if ( typeof eIdOrTabId === 'object' ) { | ||
options = eIdOrTabId; | ||
eIdOrTabId = id; | ||
} | ||
// new Client(eIdOrTabId,options) is default. | ||
if ( !options ) { | ||
options = {}; | ||
} | ||
const np = options.namespace || 'default'; | ||
let port; | ||
@@ -21,7 +37,16 @@ | ||
case 'string': | ||
port = runtime.connect( eIdOrTabId ); | ||
port = runtime.connect( eIdOrTabId , { | ||
name : JSON.stringify( { | ||
_namespace : options.namespace | ||
} ) | ||
} ); | ||
break; | ||
case 'number': | ||
port = chrome.tabs.connect( eIdOrTabId , { frameId } ); | ||
port = chrome.tabs.connect( eIdOrTabId , { | ||
frameId : options.frameId , | ||
name : JSON.stringify( { | ||
namespace : np | ||
} ) | ||
} ); | ||
break; | ||
@@ -34,3 +59,4 @@ | ||
super( port ); | ||
this.namespace = np; | ||
} | ||
}; |
@@ -13,2 +13,4 @@ import Client from './client'; | ||
* | ||
* @param {String} [options.namespace] - 此客户端所属的服务端命名空间 | ||
* | ||
* @param {String} options.name - 消息名称 | ||
@@ -19,9 +21,6 @@ * @param {*} [options.data] - 消息数据 | ||
function send( options ) { | ||
let { eId , tabId , frameId , name , data , needResponse} = options; | ||
let client; | ||
if ( tabId ) { | ||
client = new Client( tabId , frameId ); | ||
} else { | ||
client = new Client( eId || id ); | ||
} | ||
let { eId , tabId , frameId , namespace, name , data , needResponse} = options; | ||
const client = new Client( tabId || eId , { frameId , namespace } ); | ||
const p = client.send( name , data , needResponse ); | ||
@@ -28,0 +27,0 @@ if ( p ) { |
@@ -6,12 +6,77 @@ import EventEmitter from 'events'; | ||
// 单例模式在这里会出问题:https://phabricator.babeljs.io/T6883 | ||
let server; | ||
/** | ||
* 一个 map,key 为 server 的 namespace,值为 server | ||
* @type {{}} | ||
*/ | ||
const serversMap = {}; | ||
runtime.onConnect.addListener( chromePort => { | ||
initServerPort( chromePort , false ); | ||
} ); | ||
let {onConnectExternal} = runtime; | ||
if ( onConnectExternal ) { | ||
onConnectExternal.addListener( chromePort => { | ||
initServerPort( chromePort , true ); | ||
} ); | ||
} | ||
/** | ||
* 初始化服务端的端口 | ||
* @param {chrome.runtime.Port} chromePort | ||
* @param {Boolean} isExternal - 此连接是否为外部连接 | ||
*/ | ||
function initServerPort( chromePort , isExternal ) { | ||
// 由 Client 发送过来的客户端一定带有 name,且 name 可转换为 JSON 并有 _namespace 属性 | ||
let options; | ||
try { | ||
options = JSON.parse( chromePort.name ); | ||
} | ||
catch ( e ) { | ||
return; | ||
} | ||
const {_namespace} = options; | ||
if ( !_namespace ) { | ||
return; | ||
} | ||
const server = new Server( _namespace ); | ||
const {ports} = server; | ||
const port = new Port( chromePort ); | ||
port.exteranl = isExternal; | ||
port.once( 'disconnect' , ()=> { | ||
ports.splice( ports.indexOf( port ) , 1 ); | ||
} ); | ||
/** | ||
* 广播消息的方法。消息将会发送到服务器的所有客户端,除了发起这个广播的客户端本身。 | ||
* @param {String} name | ||
* @param {*} [data] | ||
*/ | ||
port.broadcast = ( name , data )=> { | ||
ports.forEach( clientPort => { | ||
if ( clientPort !== port ) { | ||
clientPort.send( name , data ); | ||
} | ||
} ); | ||
}; | ||
ports.push( port ); | ||
server.emit( 'connect' , port ); | ||
} | ||
export default class Server extends EventEmitter { | ||
constructor() { | ||
constructor( namespace = 'default' ) { | ||
super(); // super() 必须被第一个执行,否则会出错 | ||
if ( server ) { | ||
return server; | ||
const already = serversMap[ namespace ]; | ||
if ( already ) { | ||
return already; | ||
} | ||
server = this; | ||
serversMap[ namespace ] = this; | ||
this.namespace = namespace; | ||
/** | ||
@@ -21,42 +86,3 @@ * 端口的集合 | ||
*/ | ||
const ports = this.ports = []; | ||
runtime.onConnect.addListener( chromePort => { | ||
initServerPort( chromePort , false ); | ||
} ); | ||
let {onConnectExternal} = runtime; | ||
if ( onConnectExternal ) { | ||
onConnectExternal.addListener( chromePort => { | ||
initServerPort( chromePort , true ); | ||
} ); | ||
} | ||
/** | ||
* 初始化服务端的端口 | ||
* @param {chrome.runtime.Port} chromePort | ||
* @param {Boolean} isExternal - 此连接是否为外部连接 | ||
*/ | ||
function initServerPort( chromePort , isExternal ) { | ||
const port = new Port( chromePort ); | ||
port.exteranl = isExternal; | ||
port.once( 'disconnect' , ()=> { | ||
ports.splice( ports.indexOf( port ) , 1 ); | ||
} ); | ||
/** | ||
* 广播消息的方法。消息将会发送到服务器的所有客户端,除了发起这个广播的客户端本身。 | ||
* @param {String} name | ||
* @param {*} [data] | ||
*/ | ||
port.broadcast = ( name , data )=> { | ||
ports.forEach( clientPort => { | ||
if ( clientPort !== port ) { | ||
clientPort.send( name , data ); | ||
} | ||
} ); | ||
}; | ||
ports.push( port ); | ||
server.emit( 'connect' , port ); | ||
} | ||
this.ports = []; | ||
} | ||
@@ -63,0 +89,0 @@ |
{ | ||
"name": "connect.io", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "chrome.runtime.connect wrapper that using Stock.IO API.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -23,3 +23,3 @@ # connect.io | ||
```js | ||
const server = new ChromeConnect.Server(); | ||
const server = new ChromeConnect.Server('optionsal namespace, default is "default"'); | ||
server.on('connect',(client)=> { | ||
@@ -86,2 +86,4 @@ | ||
namespace:'default', | ||
name:'your msg name', | ||
@@ -95,3 +97,6 @@ data:{ your:'data' }, | ||
// long-lived connections | ||
const client = new ChromeConnect.Client('optional extensions or apps id, or tabId and frameId. default value is chrome.runtime.id'); | ||
const client = new ChromeConnect.Client('optional extensions or apps id. default value is chrome.runtime.id',{ | ||
frameId:0, // only used when first argument is tabId | ||
namespace:'' | ||
}); | ||
@@ -98,0 +103,0 @@ client.on('welcome',(data,resolve,reject) => { |
Sorry, the diff of this file is not supported yet
142600
2620
132