Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite73a57d

Browse files
author
Eric Jiang
committed
feat(): example client/server
1 parent92c1d88 commite73a57d

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

‎example-client.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
constSocket=require('simple-websocket')
2+
const{ Protocol}=require('./')
3+
4+
constsocket=newSocket('ws://localhost:3000')
5+
6+
socket.on('connect',()=>{
7+
global.alice=newProtocol({connect:()=>socket})
8+
9+
alice.call('hey joe!',['argument'],(message)=>{
10+
console.log('got response!',message)
11+
})
12+
})

‎example-server.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
constServer=require('simple-websocket/server')
2+
const{ Protocol}=require('./')
3+
4+
constserver=newServer({port:3000})
5+
6+
server.on('connection',(socket)=>{
7+
global.bob=newProtocol({connect:()=>socket})
8+
9+
bob.command('hey joe!',(command,reply)=>{
10+
console.log('got command',command)
11+
reply(null,['results1','results2'])
12+
})
13+
})

‎index.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@ const { Duplex } = require('readable-stream')
22
constmessages=require('./messages')
33
constcrypto=require('crypto')
44
constvarint=require('varint')
5+
constpump=require('pump')
56

67
classProtocolextendsDuplex{
78
constructor(opts){
89
super()
10+
if('function'!==typeofopts.connect){
11+
thrownewTypeError('Connection expects a opts.connect(...) factory')
12+
}
13+
914
this.pending={}
15+
this.createConnection=opts.connect
16+
17+
constwire=this.createConnection(this)
18+
if(wire){
19+
pump(wire,this,wire)
20+
}
1021
}
1122

1223
_read(size){

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"homepage":"https://github.com/secure-local-node/remote-protocol-stream#readme",
2525
"dependencies": {
2626
"protocol-buffers-encodings":"^1.1.0",
27+
"pump":"^3.0.0",
2728
"readable-stream":"^3.3.0",
2829
"varint":"^5.0.0"
2930
},

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp