connect.io
Advanced tools
Comparing version 0.1.10 to 0.2.0
@@ -31,3 +31,3 @@ import EventEmitter from 'events'; | ||
port.onMessage.addListener( msg => { | ||
const {name,data,id} = msg; | ||
const {id} = msg; | ||
@@ -39,3 +39,3 @@ // 如果在字典里找到了对应 id 的回调函数,那么说明这个消息是由本地端口发送的并有回调函数 | ||
delete waitingResponseMsg[ id ]; | ||
cb( data ); | ||
cb( msg.error , msg.response ); | ||
} else { | ||
@@ -47,12 +47,13 @@ let sent , sendResponse; | ||
* 发送处理结果至远程端口。这个函数只能被调用一次。 | ||
* @param {*} [error] - 错误 | ||
* @param {*} [response] | ||
*/ | ||
sendResponse = response => { | ||
sendResponse = ( error , response ) => { | ||
if ( sent ) { | ||
console.warn( `Event "${eventName}" was already response.` ); | ||
console.warn( `Message "${msg.name}" was already response.` ); | ||
return; | ||
} | ||
sent = true; | ||
// 发送回执时,此消息是没有 name 的 | ||
port.postMessage( { id , data : response } ); | ||
// 发送回执 | ||
port.postMessage( { id , response , error } ); | ||
}; | ||
@@ -63,3 +64,3 @@ } else { | ||
this.emit( name , data , sendResponse ); | ||
this.emit( msg.name , msg.data , sendResponse ); | ||
} | ||
@@ -77,9 +78,9 @@ } ); | ||
* 当连接断开时,告诉所有等待响应的消息一个错误 | ||
* @param {Boolean} isRemote - 连接是否是被远程端口断开的 | ||
* @param {Boolean} isByOtherSide - 连接是否是被另一端断开的 | ||
*/ | ||
isRemote => { | ||
isByOtherSide => { | ||
this.disconnected = true; | ||
this.disconnect = noop; | ||
for ( let key in waitingResponseMsg ) { | ||
waitingResponseMsg[ key ]( undefined , `Connection has been disconnected by ${isRemote ? 'Server' : 'Client'}.` ); | ||
waitingResponseMsg[ key ]( new Error( `Connection has been disconnected by ${isByOtherSide ? 'the other side' : 'yourself.'}.` ) ); | ||
delete waitingResponseMsg[ key ]; | ||
@@ -129,2 +130,3 @@ } | ||
* 用一个类来描述 port 之间传递的消息。 | ||
* 消息分为两种:请求消息与响应消息。 | ||
* | ||
@@ -140,3 +142,7 @@ * 当本地端口将数据发送至远程端口时, | ||
* @property {*} data - 消息携带的数据 | ||
* | ||
* @property {String} id - 消息的 uuid | ||
* | ||
* @property {*} response - 如果消息是一次响应,则此属性为远程端口响应的数据 | ||
* @property {*} error - 如果消息是一次响应,则此属性为远程端口响应的错误消息 | ||
*/ |
{ | ||
"name": "connect.io", | ||
"version": "0.1.10", | ||
"version": "0.2.0", | ||
"description": "chrome.runtime.connect wrapper that using Stock.IO API.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -13,26 +13,2 @@ # connect.io | ||
Client(content-scripts.html): | ||
```html | ||
<script src="node_modules/connect.io/dist/connect.js"></script> | ||
<script> | ||
const client = new ChromeConnect.Client('optional extensions or apps id or tabId, default value is chrome.runtime.id'); | ||
client.on('welcome',(msg) => { | ||
console.log(msg); // 'hello world' | ||
}); | ||
// get message acknowledgements | ||
client.send('report clients number', (number, error) => { | ||
// When connection has been disconnected until get response, then error will be a string. | ||
// It's often happened when you connect to a tab from extension but you don't have permission. (etc chrome:// 、chrome-extension://) | ||
if( error ) { | ||
throw new Error(error); | ||
} | ||
console.log(number); // 100 | ||
}); | ||
</script> | ||
``` | ||
background.html: | ||
@@ -46,3 +22,3 @@ | ||
if (client.exteranl && client.port.sender.url === blackList) { | ||
if (client.exteranl && client.port.sender.url === YourBlackList) { | ||
client.disconnect(); | ||
@@ -53,3 +29,10 @@ return; | ||
// Only send message to this connection client. | ||
client.send('welcome','hello world'); | ||
// You also can get Client response. | ||
client.send('welcome','hello world',(error,response) { | ||
if(error){ | ||
console.log(error); // print "I'm not happy." | ||
}else{ | ||
console.log(response); // print "Thanks!" | ||
} | ||
}); | ||
@@ -59,15 +42,58 @@ // Sending a message to everyone else except for the connection that starts it. | ||
// Sending acknowledgements | ||
// Sending response to client | ||
client.on('report clients number', (data, sendResponse) => { | ||
console.log(data); // when no data send to server, the data argument will be undefined | ||
sendResponse(100); | ||
sendResponse(null ,server.ports.length); | ||
}); | ||
client.on('disconect', () => { | ||
// handle connection disconnect on Server | ||
client.once('disconnect', isOtherSide => { | ||
// Sending messge to every connection. | ||
server.send('Someone out'); | ||
server.send(isOtherSide ? 'Someone out by himself.' : 'I knock it out.'); | ||
}); | ||
}); | ||
</script> | ||
``` | ||
Client(content-scripts.html): | ||
```html | ||
<script src="node_modules/connect.io/dist/connect.js"></script> | ||
<script> | ||
const client = new ChromeConnect.Client('optional extensions or apps id or tabId, default value is chrome.runtime.id'); | ||
client.on('welcome',(data,sendResponse) => { | ||
console.log(data); // 'hello world' | ||
// if you want, you can send a response to Server. | ||
sendResponse( null, 'Thanks!' ); | ||
// or you can send an error as a rejection. | ||
sendResponse('I\'m not happy.'); | ||
}); | ||
client.on('join',function(data){ | ||
console.log(data); // "new client joined." | ||
}) | ||
// get Server response | ||
client.send('report clients number', (error,response) => { | ||
// Don't forget to handle error. | ||
if( error ) { | ||
throw error; | ||
} | ||
console.log(response); // 1 | ||
}); | ||
client.on('Someone out',()=>{ | ||
// ... | ||
}); | ||
// handle connection disconnect on Client | ||
client.once('disconnect', isOtherSide => { | ||
console.log('Connection disconnected by ', isOtherSide ? 'the other side' : 'myself', '.'); | ||
}); | ||
// disconnect the connection. | ||
client.disconnect(); | ||
</script> | ||
``` |
Sorry, the diff of this file is not supported yet
97961
1594
96