add .gitignore and .prettierrc.js

This commit is contained in:
陆柯 2021-01-26 19:04:15 +08:00
parent 6ea84fc811
commit 90c18ea708
4 changed files with 129 additions and 87 deletions

17
.gitignore vendored Normal file
View File

@ -0,0 +1,17 @@
.DS_Store
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
/test/unit/coverage/
/test/e2e/reports/
selenium-debug.log
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
/package-lock.json

16
.prettierrc.js Normal file
View File

@ -0,0 +1,16 @@
/*
VSCode Prettier 有效建议一直要有本配置文件否则不同版本的 Prettier 的默认配置会不同例如 TrailingComma
VSCode Prettier Standard 无效似乎是集成了不能修改的配置
*/
module.exports = {
printWidth: 160, // default 80
tabWidth: 2, // default 2
useTabs: false,
semi: false, // default true
singleQuote: true, // default false
trailingComma: 'es5', // none (default in v 1.*), es5 (default in v2.0.0), all
bracketSpacing: true, // default true
jsxBracketSameLine: false, // default false
arrowParens: 'always', // avoid (default in v1.9.0), always (default since v2.0.0)
quoteProps: 'as-needed', // as-needed (default), consistent, preserve
}

113
index.js
View File

@ -4,7 +4,9 @@ const sleep = (ms)=>new Promise((resolve, reject)=>setTimeout(resolve, ms))
// 建议把 onopen, onerror, onclose, onmessage 保留给系统使用例如reconnecting。用户使用 on(event, listener) // 建议把 onopen, onerror, onclose, onmessage 保留给系统使用例如reconnecting。用户使用 on(event, listener)
const DAD = module.exports = class TicSocket extends ws { const DAD = (module.exports = class RpcSocket extends (
ws
) {
constructor(address, protocols, options) { constructor(address, protocols, options) {
let ws = super(address, protocols, options) let ws = super(address, protocols, options)
DAD.upgrade(ws) DAD.upgrade(ws)
@ -13,7 +15,49 @@ const DAD = module.exports = class TicSocket extends ws {
static upgrade(ws) { static upgrade(ws) {
ws.__proto__ = DAD.prototype ws.__proto__ = DAD.prototype
ws.sid = randomBytes(16).toString('hex') ws.sid = randomBytes(16).toString('hex')
ws.onMessage() ws.onmessage = async ({ data }) => {
// console.log('onmessage 被调用')
try {
let obj = JSON.parse(data)
// console.log('message data parsed to obj=',obj)
switch (obj.type) {
case 'NOTIFY': // 接收到同步的远程调用
// console.log(`NOTIFY message: rpc=${obj.rpc}, param=${JSON.stringify(obj.param)}`)
if (ws.hasOwnProperty(obj.rpc))
// 作为rpc调用
ws[obj.rpc](obj.param)
else if (ws.eventNames().indexOf(obj.rpc) >= 0)
// 作为event调用
ws.emit(obj.rpc, obj.param)
else console.log('onmessage unknown notification')
case 'REQUEST': // 接收到异步的远程调用
// console.log(`被动方 收到 REQUEST: rpc=${obj.rpc}, param=${JSON.stringify(obj.param)}`)
let result
if (ws.hasOwnProperty(obj.rpc)) {
result = await ws[obj.rpc](obj.param)
} else {
result = { _state: 'error', error: 'unknown rpc' }
}
if (obj.randomEvent) {
ws.send(
JSON.stringify({
type: 'REPLY',
randomEvent: obj.randomEvent,
result,
})
) // 返回结果给远程
}
case 'REPLY': // 接收到远程返回的结果
// console.log('主动方 收到 REPLY: result=', obj.result)
ws.emit(obj.randomEvent, obj.result)
default:
}
} catch (exception) {
console.error('Invalid socket message received!')
return
}
}
return ws return ws
} }
@ -36,14 +80,17 @@ const DAD = module.exports = class TicSocket extends ws {
}) })
} }
notify({rpc, param, options, cb}={}){ // 发起同步的远程调用 notify({ rpc, param, options, cb } = {}) {
// 发起同步的远程调用
this.send(JSON.stringify({ type: 'NOTIFY', rpc, param, options, cb })) this.send(JSON.stringify({ type: 'NOTIFY', rpc, param, options, cb }))
} }
requestAsync( { rpc, param, ack, timeout = 5000 } = {} ) { // 发起异步的远程调用(rpc) requestAsync({ rpc, param, ack, timeout = 5000 } = {}) {
// 发起异步的远程调用(rpc)
let randomEvent = randomBytes(16).toString('hex') let randomEvent = randomBytes(16).toString('hex')
// console.log('randomEvent is randomized: ', randomEvent) // console.log('randomEvent is randomized: ', randomEvent)
if ( typeof(ack) === 'function' ){ // 有回调 if (typeof ack === 'function') {
// 有回调
this.send(JSON.stringify({ type: 'REQUEST', rpc, param, randomEvent }), () => { this.send(JSON.stringify({ type: 'REQUEST', rpc, param, randomEvent }), () => {
// console.log('in callback, randomEvent=', randomEvent) // console.log('in callback, randomEvent=', randomEvent)
this.once(randomEvent, ack) this.once(randomEvent, ack)
@ -54,7 +101,8 @@ const DAD = module.exports = class TicSocket extends ws {
} }
}, timeout) }, timeout)
}) })
} else { // 没有回调 } else {
// 没有回调
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.send(JSON.stringify({ type: 'REQUEST', rpc, param, randomEvent }), () => { this.send(JSON.stringify({ type: 'REQUEST', rpc, param, randomEvent }), () => {
this.once(randomEvent, resolve) this.once(randomEvent, resolve)
@ -68,65 +116,26 @@ const DAD = module.exports = class TicSocket extends ws {
}) })
} }
} }
})
onMessage(){ // 接收到远程消息
this.onmessage = async ({data})=>{ // 不能直接定义成一个函数,而要现场赋值。因为里面用到了 this 还是指向 ws.prototype 而不是socket本身
// console.log('onmessage 被调用')
try {
let obj = JSON.parse(data)
// console.log('message data parsed to obj=',obj)
switch (obj.type) {
case 'NOTIFY': // 接收到同步的远程调用
// console.log(`NOTIFY message: rpc=${obj.rpc}, param=${JSON.stringify(obj.param)}`)
if (this.hasOwnProperty(obj.rpc)) // 作为rpc调用
this[obj.rpc](obj.param)
else if (this.eventNames().indexOf(obj.rpc)>=0) // 作为event调用
this.emit(obj.rpc, obj.param)
else
console.log('onmessage unknown notification')
case 'REQUEST': // 接收到异步的远程调用
// console.log(`被动方 收到 REQUEST: rpc=${obj.rpc}, param=${JSON.stringify(obj.param)}`)
let result
if (this.hasOwnProperty(obj.rpc)) {
result = await this[obj.rpc](obj.param)
}else {
result = {_state:'error', error:'unknown rpc'}
}
if (obj.randomEvent){
this.send(JSON.stringify({type:'REPLY', randomEvent: obj.randomEvent, result})) // 返回结果给远程
}
case 'REPLY': // 接收到远程返回的结果
// console.log('主动方 收到 REPLY: result=', obj.result)
this.emit(obj.randomEvent, obj.result)
default:
}
}catch(exception){
console.error('Invalid socket message received!')
return
}
}
}
}
// (async ()=>{ // (async ()=>{
// new TicSocket('http://localhost:9007') // new RpcSocket('http://localhost:9007')
// await sleep(20000) // await sleep(20000)
// new TicSocket.Server({port:9007}) // new RpcSocket.Server({port:9007})
// await sleep(20000) // await sleep(20000)
// })() // })()
/* /*
//TicSocket=require('./Ling/TicSocket.js'); //RpcSocket=require('./Ling/RpcSocket.js');
server = new TicSocket.Server({port:6000}); server = new RpcSocket.Server({port:6000});
var serverSocket; server.on('connection', (socket)=>{console.log('socket coming'); var serverSocket; server.on('connection', (socket)=>{console.log('socket coming');
serverSocket = TicSocket.upgrade(socket).on('close', (code, reason)=>console.log('socket is closed for code and reason: ', code, reason)) serverSocket = RpcSocket.upgrade(socket).on('close', (code, reason)=>console.log('socket is closed for code and reason: ', code, reason))
.on('error', (errer)=>console.log('socket has error: ', error)) .on('error', (errer)=>console.log('socket has error: ', error))
}) })
TicSocket.connectAsync('http://localhost:6000').then((clientSocket)=>{ RpcSocket.connectAsync('http://localhost:6000').then((clientSocket)=>{
clientSocket.request('testfunc', {height:99}, (result)=>console.log('using request: ', result), 3000) clientSocket.request('testfunc', {height:99}, (result)=>console.log('using request: ', result), 3000)
return clientSocket return clientSocket
}).then((clientSocket)=>{ }).then((clientSocket)=>{

View File

@ -1,5 +1,5 @@
{ {
"name": "websocket", "name": "so.rpcsocket",
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"dependencies": { "dependencies": {