forked from crossbario/autobahn-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented pluggable serializers and msgpack serialization
- Loading branch information
Showing
10 changed files
with
208 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// AutobahnJS - http://autobahn.ws, http://wamp.ws | ||
// | ||
// A JavaScript library for WAMP ("The Web Application Messaging Protocol"). | ||
// | ||
// Copyright (C) 2011-2016 Tavendo GmbH, http://tavendo.com | ||
// | ||
// Licensed under the MIT License. | ||
// http://www.opensource.org/licenses/mit-license.php | ||
// | ||
/////////////////////////////////////////////////////////////////////////////// | ||
|
||
|
||
function JSONSerializer(replacer, reviver) { | ||
this.replacer = replacer; | ||
this.reviver = reviver; | ||
this.SERIALIZER_ID = 'json'; | ||
this.BINARY = false; | ||
} | ||
|
||
JSONSerializer.prototype.serialize = function (obj) { | ||
return JSON.stringify(obj, this.replacer); | ||
}; | ||
|
||
JSONSerializer.prototype.unserialize = function (payload) { | ||
return JSON.parse(payload, this.reviver); | ||
}; | ||
|
||
exports.JSONSerializer = JSONSerializer; | ||
|
||
|
||
try { | ||
var msgpack = require('msgpack-lite'); | ||
|
||
function MsgpackSerializer() { | ||
// https://github.com/mcollina/msgpack5 | ||
this.SERIALIZER_ID = 'msgpack'; | ||
this.BINARY = true; | ||
} | ||
|
||
MsgpackSerializer.prototype.serialize = function (obj) { | ||
return msgpack.encode(obj); | ||
}; | ||
|
||
MsgpackSerializer.prototype.unserialize = function (payload) { | ||
return msgpack.decode(payload); | ||
}; | ||
|
||
exports.MsgpackSerializer = MsgpackSerializer; | ||
} catch (err) { | ||
// msgpack-lite not installed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// AutobahnJS - http://autobahn.ws, http://wamp.ws | ||
// | ||
// A JavaScript library for WAMP ("The Web Application Messaging Protocol"). | ||
// | ||
// Copyright (C) 2011-2014 Tavendo GmbH, http://tavendo.com | ||
// | ||
// Licensed under the MIT License. | ||
// http://www.opensource.org/licenses/mit-license.php | ||
// | ||
/////////////////////////////////////////////////////////////////////////////// | ||
|
||
var autobahn = require('./../index.js'); | ||
var testutil = require('./testutil.js'); | ||
|
||
|
||
exports.testMsgpackSerialization = function (testcase) { | ||
|
||
testcase.expect(1); | ||
|
||
var test = new testutil.Testlog("test/test_msgpack_serialization.txt"); | ||
|
||
var serializer = new autobahn.serializer.MsgpackSerializer(); | ||
|
||
var config = { | ||
url: testutil.config.url, | ||
realm: testutil.config.realm, | ||
serializers: [serializer] | ||
}; | ||
var connection = new autobahn.Connection(config); | ||
|
||
connection.onopen = function (session) { | ||
|
||
test.log('Connected'); | ||
|
||
function echo(args) { | ||
return args[0]; | ||
} | ||
|
||
var endpoints = { | ||
'com.myapp.echo': echo | ||
}; | ||
|
||
var pl1 = []; | ||
|
||
for (var uri in endpoints) { | ||
pl1.push(session.register(uri, endpoints[uri])); | ||
} | ||
|
||
autobahn.when.all(pl1).then( | ||
function () { | ||
test.log("All registered."); | ||
test.log("Serializer ID: " + session._socket.serializer.SERIALIZER_ID); | ||
|
||
var pl2 = []; | ||
|
||
var vals1 = [1.7, "hello", [1, 2, -3], {a: 5, b: "hello2"}, null]; | ||
|
||
for (var i = 0; i < vals1.length; ++i) { | ||
|
||
pl2.push(session.call('com.myapp.echo', [vals1[i]]).then( | ||
function (res) { | ||
test.log("Result:", res); | ||
}, | ||
function (err) { | ||
test.log("Error:", err.error, err.args, err.kwargs); | ||
} | ||
)); | ||
} | ||
|
||
autobahn.when.all(pl2).then(function () { | ||
test.log("All finished."); | ||
connection.close(); | ||
|
||
var chk = test.check(); | ||
testcase.ok(!chk, chk); | ||
testcase.done(); | ||
}); | ||
}, | ||
function () { | ||
test.log("Registration failed!", arguments); | ||
} | ||
); | ||
}; | ||
|
||
connection.open(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
0 "Connected" | ||
1 "All registered." | ||
2 "Serializer ID: msgpack" | ||
3 "Result:" 1.7 | ||
4 "Result:" "hello" | ||
5 "Result:" [1,2,-3] | ||
6 "Result:" {"a":5,"b":"hello2"} | ||
7 "Result:" null | ||
8 "All finished." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters