Skip to content

Commit

Permalink
move internal wamp api
Browse files Browse the repository at this point in the history
  • Loading branch information
kalmyk committed Mar 6, 2019
1 parent 78ea116 commit be74616
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 48 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ router.listenWAMP({server: httpServer, path: "/wss"})

and correspondingly the web socket client connection will look like as
```javascript
var autobahn = require('autobahn')
let autobahn = require('autobahn')
let connection = new autobahn.Connection({
url: 'ws:localhost:5000/wss',
realm: 'realm1'
Expand Down
62 changes: 62 additions & 0 deletions democli/mass-publish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// AUTOBAHN_DEBUG = true;
const autobahn = require('autobahn')
const program = require('commander')

program
.option('-s, --server <server>', 'Server URI address', 'ws://127.0.0.1:9000/wamp')
.parse(process.argv)

console.log('connect to server:', program.server)

var user = "joe"
var key = "joe-secret"

// this callback is fired during authentication
function onchallenge (session, method, extra) {
if (method === "ticket") {
return key
} else {
throw "don't know how to authenticate using '" + method + "'"
}
}

var connection = new autobahn.Connection({
url: program.server,
realm: 'realm1',
authmethods: ["ticket", "wampcra"],
authid: user,
onchallenge: onchallenge
})

const msgCount = 100000

connection.onopen = function (session, details) {

session.log("Session open.")

var starttime = Date.now()
var res = []
for (var i=0; i<msgCount; i++) {
res.push(session.publish('com.myapp.topic1', [], {field1:'some long value', field2:12345}, { acknowledge : true }).then(
function(publication) {
// console.log("published, publication ID is ", publication);
},
function(error) {
console.log("publication error", error)
return Promise.resolve(true)
}
))
}

// when progressive call and acknowledge publish done
Promise.all(res).then(function () {
console.log('total rec/sec:', msgCount/(Date.now() - starttime)*1000 )
connection.close();
})
}

connection.onclose = function (reason, details) {
console.log("close connection:", reason, details)
}

connection.open()
3 changes: 3 additions & 0 deletions lib/mqtt/gate.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class MqttGate extends BaseGate {
} else
if (cmd.data.kwargs !== undefined) {
payload = new Buffer(JSON.stringify(cmd.data.kwargs))
} else
if (cmd.data.kv !== undefined) {
payload = new Buffer(JSON.stringify(cmd.data.kv))
}
session.send({
topic: cmd.uri,
Expand Down
32 changes: 16 additions & 16 deletions lib/realm.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict'

let Qlobber = require('qlobber').Qlobber
const Qlobber = require('qlobber').Qlobber

let { SESSION_JOIN, SESSION_LEAVE, RESULT_EMIT, ON_SUBSCRIBED, ON_UNSUBSCRIBED,
const { SESSION_JOIN, SESSION_LEAVE, RESULT_EMIT, ON_SUBSCRIBED, ON_UNSUBSCRIBED,
ON_REGISTERED, ON_UNREGISTERED } = require('./messages')

let errorCodes = require('./realm_error').errorCodes
let RealmError = require('./realm_error').RealmError
let Api = require('./api')
let KeyStorage = require('./storage')
let tools = require('./tools')
const errorCodes = require('./realm_error').errorCodes
const RealmError = require('./realm_error').RealmError
const WampApi = require('./wamp/api')
const KeyStorage = require('./storage')
const tools = require('./tools')

/*
message fields description
Expand Down Expand Up @@ -362,22 +362,13 @@ class EnginePush extends EngineBase {

class BaseRealm {
constructor (router, rpc, push, storage) {
this._api = null
this._sessions = new Map() // session by sessionId
this._storage = storage
this._router = router
this.rpc = rpc
this.push = push
}

api () {
if (!this._api) {
this._api = new Api(this)
this.joinSession(this._api)
}
return this._api
}

doEcho (session, cmd) {
let a = new Actor(session, cmd)
a.acknowledged()
Expand Down Expand Up @@ -548,6 +539,15 @@ class Realm extends BaseRealm {
new EnginePush(),
new KeyStorage()
)
this._api = null
}

api () {
if (!this._api) {
this._api = new WampApi(this)
this.joinSession(this._api)
}
return this._api
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ function Session (gate, sender, sessionId) {
let tasksRequested = 0

/**
trace commands
trace commands
[id] => actor
*/
let sTrace = new Map()

/**
subscribtion commands
subscribtion commands
[id] => actor
*/
let sSub = new Map()
Expand Down
16 changes: 8 additions & 8 deletions lib/api.js → lib/wamp/api.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

const inherits = require('util').inherits
const { RESULT_EMIT } = require('./messages')
const dparse = require('./wamp/dparse')
const Session = require('./session')
const tools = require('./tools')
const { RESULT_EMIT } = require('../messages')
const dparse = require('./dparse')
const Session = require('../session')
const tools = require('../tools')

function Api (realm) {
function WampApi (realm) {
Session.call(this)

this.gate = this
Expand Down Expand Up @@ -92,9 +92,9 @@ function Api (realm) {
}

this.getGateProtocol = function () {
return 'internal.api'
return 'internal.wamp.api'
}
}
inherits(Api, Session)
inherits(WampApi, Session)

module.exports = Api
module.exports = WampApi
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fox-wamp",
"version": "0.5.10",
"version": "0.5.11",
"description": "Web Application Message Router/Server WAMP/MQTT",
"author": {
"name": "Anatoly Tsapkov",
Expand Down Expand Up @@ -34,9 +34,8 @@
"chai": "^4.2.0",
"chai-as-promised": "*",
"chai-spies": "*",
"eslint": "^5.14.1",
"eslint-plugin-mocha": "^5.3.0",
"randombytes": "^2.1.0"
"eslint": "^5.15.1",
"eslint-plugin-mocha": "^5.3.0"
},
"main": "index.js",
"engines": {
Expand Down

0 comments on commit be74616

Please sign in to comment.