connect.io
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -89,23 +89,31 @@ import EventEmitter from 'events'; | ||
* @param {String} name - 消息名称 | ||
* @param {*} [data] - 数据 | ||
* @param {Function} [onComplete] - 完成时的回调函数 | ||
* @param {*} [data] 数据 | ||
* @param {Boolean} [needResponse] - 如果是 true,则此方法返回一个 Promise,当得到相应时会被 resolve 或 reject。 | ||
* | ||
* @example | ||
* send('x',{my:'data'}) | ||
* send('x',(res)=>{}) | ||
* send('x',{my:'data'},(res)=>{}) | ||
* send('name','data',true) | ||
* send('name',true) - 这种情况下,data 为 undefined,needResponse 为 true | ||
* send('name','data') | ||
* send('name') | ||
*/ | ||
send( name , data , onComplete ) { | ||
if ( typeof data === 'function' ) { | ||
onComplete = data; | ||
send( name , data , needResponse ) { | ||
if ( data === true && arguments.length === 2 ) { | ||
data = undefined; | ||
needResponse = true; | ||
} | ||
const msg = { name , data }; | ||
if ( onComplete ) { | ||
// 给消息带上 uuid,这样就能通过这个 id 定位到本地等待响应的回调函数 | ||
this._waiting[ msg.id = uuid() ] = onComplete; | ||
let p; | ||
if ( needResponse ) { | ||
p = new Promise( ( resolve , reject )=> { | ||
this._waiting[ msg.id = uuid() ] = ( error , response )=> { | ||
if ( error ) { | ||
reject( error ); | ||
} else { | ||
resolve( response ); | ||
} | ||
}; | ||
} ); | ||
} | ||
this.port.postMessage( msg ); | ||
return p; | ||
} | ||
@@ -112,0 +120,0 @@ |
{ | ||
"name": "connect.io", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "chrome.runtime.connect wrapper that using Stock.IO API.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
# connect.io | ||
Real-time bidirectional event-based communication in Chrome extensions or Apps inspired by [Socket.IO](http://socket.io/). | ||
Real-time bidirectional event-based communication in Chrome extensions or Apps inspired by [Socket.IO](http://socket.io/). | ||
@@ -27,11 +27,28 @@ ## Install | ||
// Only send message to this connection client. | ||
// 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!" | ||
} | ||
}); | ||
// If you want get response, pass "true" as the last argument, | ||
// then "client.send()" will return a promise. | ||
// Otherwise, "client.send()" just return undefined. | ||
client | ||
.send('welcome','hello world',true) | ||
.then( | ||
response => console.log(response), // print "Thanks!" | ||
error => console.log(error) // print "I'm not happy." | ||
); | ||
// Note: if you just want send a "true" to the other side and don't want response, | ||
// You must do: | ||
client.send('send true and do not need response',true,false); | ||
// I recommend you use 1 to instead of true: | ||
client.send('use 1 to instead of true',1); | ||
// and in Server: | ||
//server.on('use 1 to instead of true',(data)=>{ | ||
// console.log(data); // 1 | ||
// if(data) { | ||
// //... | ||
// } | ||
//}); | ||
// Sending a message to everyone else except for the connection that starts it. | ||
@@ -71,15 +88,12 @@ client.broadcast('join','new client joined.'); | ||
console.log(data); // "new client joined." | ||
}) | ||
}); | ||
// get Server response | ||
client.send('report clients number', (error,response) => { | ||
// get Server response. | ||
client | ||
.send('report clients number',true) | ||
.then( | ||
res => console.log(res) , // 1 | ||
error => console.log(error) | ||
); | ||
// Don't forget to handle error. | ||
if( error ) { | ||
throw error; | ||
} | ||
console.log(response); // 1 | ||
}); | ||
client.on('Someone out',()=>{ | ||
@@ -86,0 +100,0 @@ // ... |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
125467
2432
110