Skip to content

Commit

Permalink
ws connection implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarftoro committed May 23, 2015
1 parent 5efb9f4 commit 246e219
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 23 deletions.
2 changes: 1 addition & 1 deletion priv/client/js/xml_viewer-ui-simple.js
Expand Up @@ -13,7 +13,7 @@ $(document).ready(function(){
console.log('bullet: closed');
};
bullet.onmessage = function(e){
alert(e.data);
console.log(e.data);
};
bullet.onheartbeat = function(){
bullet.send('ping');
Expand Down
88 changes: 66 additions & 22 deletions priv/client/js/xml_viewer-ui.js
@@ -1,13 +1,12 @@
var GenericWrapper = React.createClass({
componentDidMount: function() {
console.log(Array.isArray(this.props.children)); // => true
(bullet);
;
},

render: function() {
return <div />;
}
});
}
});

var LoadXMLButton = React.createClass({
getInitialState: function() {
Expand Down Expand Up @@ -36,24 +35,69 @@ var LoadXMLButton = React.createClass({
// bullet communication//
/////////////////////////

var bullet = function(){
var bullet = $.bullet('ws://localhost:8181/ws');
bullet.onopen = function(){
console.log('bullet: opened');
};
bullet.ondisconnect = function(){
console.log('bullet: disconnected');
};
bullet.onclose = function(){
console.log('bullet: closed');
};
bullet.onmessage = function(e){
alert(e.data);
};
bullet.onheartbeat = function(){
bullet.send('ping');
}
}();
// var bullet = function(){
// var bullet = $.bullet('ws://localhost:8181/ws');
// bullet.onopen = function(){
// console.log('bullet: opened');
// };
// bullet.ondisconnect = function(){
// console.log('bullet: disconnected');
// };
// bullet.onclose = function(){
// console.log('bullet: closed');
// };
// bullet.onmessage = function(e){
// alert(e.data);
// };
// bullet.onheartbeat = function(){
// bullet.send('ping');
// }
// };

///////////////////////
//Auxiliar functions //
///////////////////////
function notify(text) {
var date = (new Date()).toLocaleString();
output.innerHTML = output.innerHTML + '[' + date + '] ' + text + '\n';
}

function onData(data) {
notify(data);
}
var bullet = function bullet(){
};

function start(url, options, notify, onData) {
var connection = $.bullet(url, options);

connection.onopen = function(){
notify('online');
};

connection.onclose = connection.ondisconnect = function(){
notify('offline');
};

connection.onmessage = function(e){
if (e.data === 'pong'){
notify('pong');
} else {
onData(e.data);
}
};

connection.onheartbeat = function(){
connection.send('ping');
notify('ping');
};
return connection;
}

connection = start("ws://" + window.location.host + "/ws", {}, notify, onData);




React.render(
<LoadXMLButton />,
Expand Down

0 comments on commit 246e219

Please sign in to comment.