-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f1d4ec
commit 8cc4681
Showing
4 changed files
with
226 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
function Message() { | ||
Object.defineProperties( | ||
this, { | ||
data: { | ||
enumerable: true, | ||
get: getData, | ||
set: setData | ||
}, | ||
type: { | ||
enumerable: true, | ||
get: getType, | ||
set: setType | ||
}, | ||
load:{ | ||
enumarable:true, | ||
writable:false, | ||
value:parse | ||
}, | ||
JSON: { | ||
enumerable: true, | ||
get: getJSON | ||
} | ||
} | ||
); | ||
|
||
var type = ''; | ||
var data = {}; | ||
|
||
function getType() { | ||
return type; | ||
} | ||
|
||
function getData() { | ||
return data; | ||
} | ||
|
||
function getJSON() { | ||
return JSON.stringify( | ||
{ | ||
type: type, | ||
data: data | ||
} | ||
); | ||
} | ||
|
||
function setType(value) { | ||
type = value; | ||
} | ||
|
||
function setData(value) { | ||
data = value; | ||
} | ||
|
||
function parse(message){ | ||
try{ | ||
var message=JSON.parse(message); | ||
type=message.type; | ||
data=message.data; | ||
}catch(err){ | ||
var badMessage=message; | ||
type='error', | ||
data={ | ||
message:'Invalid JSON response format', | ||
err:err, | ||
response:badMessage | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports=Message; |
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 |
---|---|---|
@@ -1,2 +1,75 @@ | ||
# js-message | ||
Normalized JS & JSON Message Protocol | ||
Normalized JS & JSON Message and event Protocol for node.js, react.js, websockets, rest api's, node-ipc, and any other protocol that might use a js object and or a JSON string. | ||
|
||
js-message allows for seamless conversion of JSON messages and events to JS objects for a normalized implementation on the server and in the client without needing to concern yourself with JSON intermediaries and custom parsers. | ||
|
||
Things are just easier when you normalize them. | ||
|
||
|
||
|method or key |type |mutable|description| | ||
|---------------|-------|-------|-----------| | ||
|type |String |true |the type of message| | ||
|data |Object |true |the message data or payload| | ||
|load |func |false |load a message from JSON, this will return a message with the type of error if not valid JSON| | ||
|JSON |String |not by user|JSON representation of the message| | ||
|
||
### Creating a Message Object | ||
|
||
```javascript | ||
|
||
var Message=require('js-message'); | ||
|
||
var myMessage=new Message; | ||
myMessage.type='message or event type'; | ||
myMessage.data.something='something'; | ||
myMessage.data.stuff=[1,2,3,4,5] | ||
|
||
console.log(myMessage.JSON); | ||
|
||
``` | ||
|
||
### Creating a Message From JSON | ||
|
||
```javascript | ||
|
||
var Message=require('js-message'); | ||
|
||
//lets say we have the above example running on | ||
//a websocket server sending js-messages as JSON | ||
// | ||
//and lets say this is the client in the browser | ||
ws.on( | ||
'message', | ||
handleMessage | ||
); | ||
|
||
handleMessage(e){ | ||
var message=new Message; | ||
message.load(e.data); | ||
|
||
console.log(message.type, message.data); | ||
} | ||
|
||
``` | ||
|
||
### Sending a Message Object via WebSocket | ||
|
||
```javascript | ||
|
||
var Message=require('js-message'); | ||
|
||
//client example, but works the same on server too! | ||
var ws=new WebSocket('ws://myawesomeWS:8000'); | ||
|
||
var myMessage=new Message; | ||
myMessage.type='setUsername'; | ||
myMessage.data.username='sideshow bob'; | ||
|
||
ws.send(myMessage.JSON); | ||
|
||
``` | ||
|
||
|
||
--- | ||
|
||
This work is licenced via the [DBAD Public Licence](http://www.dbad-license.org/). |
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,27 @@ | ||
# DON'T BE A DICK PUBLIC LICENSE | ||
|
||
> Version 1, December 2009 | ||
> Copyright (C) 2009 Philip Sturgeon <email@philsturgeon.co.uk> | ||
Everyone is permitted to copy and distribute verbatim or modified | ||
copies of this license document, and changing it is allowed as long | ||
as the name is changed. | ||
|
||
> DON'T BE A DICK PUBLIC LICENSE | ||
> TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | ||
1. Do whatever you like with the original work, just don't be a dick. | ||
|
||
Being a dick includes - but is not limited to - the following instances: | ||
|
||
1a. Outright copyright infringement - Don't just copy this and change the name. | ||
1b. Selling the unmodified original with no work done what-so-ever, that's REALLY being a dick. | ||
1c. Modifying the original work to contain hidden harmful content. That would make you a PROPER dick. | ||
|
||
2. If you become rich through modifications, related works/services, or supporting the original work, | ||
share the love. Only a dick would make loads off this work and not buy the original work's | ||
creator(s) a pint. | ||
|
||
3. Code is provided with no warranty. Using somebody else's code and bitching when it goes wrong makes | ||
you a DONKEY dick. Fix the problem yourself. A non-dick would submit the fix back. |
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,54 @@ | ||
{ | ||
"name": "js-message", | ||
"version": "1.0.0", | ||
"description": "normalized JS Object and JSON message and event protocol for node.js, vanialla js, react.js, components, actions, stores and dispatchers", | ||
"main": "WS.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "node devServer.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/RIAEvangelist/js-message.git" | ||
}, | ||
"engines": { | ||
"node": ">=0.6.0" | ||
}, | ||
"keywords": [ | ||
"message", | ||
"normalize", | ||
"events", | ||
"js", | ||
"json", | ||
"protocol", | ||
"ipc", | ||
"node", | ||
"nodejs", | ||
"node.js", | ||
"react", | ||
"react.js", | ||
"reactjs", | ||
"websocket", | ||
"websockets", | ||
"web", | ||
"socket", | ||
"sockets", | ||
"ws", | ||
"flux", | ||
"reflux", | ||
"component", | ||
"components", | ||
"store", | ||
"stores", | ||
"action", | ||
"actions" | ||
], | ||
"author": "Brandon Nozaki Miller", | ||
"license": "DBAD", | ||
"bugs": { | ||
"url": "https://github.com/RIAEvangelist/js-message/issues" | ||
}, | ||
"homepage": "https://github.com/RIAEvangelist/js-message#readme", | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |