chat example working with SINs

This commit is contained in:
Manuel Araoz 2014-08-01 13:26:17 -03:00
parent fe2f6ec6c4
commit fc5daaa988
3 changed files with 445 additions and 17 deletions

View File

@ -63,7 +63,8 @@ module.exports.broadcastSyncInfo = function(historicSync) {
module.exports.broadcastMessage = function(from, to, ts, message) { module.exports.broadcastMessage = function(from, to, ts, message) {
if (ios) { if (ios) {
console.log('sending message '+to); console.log('sending message '+to);
ios.sockets.in(to).emit(from, { ios.sockets.in(to).emit('message', {
from: from,
payload: message, payload: message,
ts: ts ts: ts
}); });

392
examples/bitcore-0.1.34.js Normal file

File diff suppressed because one or more lines are too long

View File

@ -22,7 +22,7 @@
form input { form input {
border: 0; border: 0;
padding: 10px; padding: 10px;
width: 90%; width: 40%;
margin-right: .5%; margin-right: .5%;
} }
form button { form button {
@ -46,31 +46,66 @@
</head> </head>
<body> <body>
<h3 id="sin">Generating SIN...</h3>
<ul id="messages"></ul> <ul id="messages"></ul>
<form action=""> <form action="">
<input id="m" autocomplete="off" /> <input id="other" placeholder="other SIN" autocomplete="off" />
<input id="m" placeholder="Text message..." autocomplete="off" />
<button>Send</button> <button>Send</button>
</form> </form>
<script src="http://localhost:3001/socket.io/socket.io.js"></script> <script src="http://localhost:3001/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script> <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script src="./bitcore-0.1.34.js"></script>
<script> <script>
var socket = io('http://localhost:3001'); $(document).ready(function()
$('form').submit(function()
{ {
var payload = $('#m').val() // load dependencies
socket.emit('message', var socket = io('http://localhost:3001');
var bitcore = require('bitcore');
var SIN = bitcore.SIN;
var util = bitcore.util;
var Key = bitcore.Key;
// show message
var show = function(msg) {
$('#messages').append($('<li>').text(msg.from+':' + JSON.stringify(msg.payload)));
};
// generate new identity
var pk = Key.generateSync();
var sin = SIN.fromPubKey(pk.public).toString('hex');
$('#sin').text('Your SIN: '+sin);
// send chat handler
$('form').submit(function()
{ {
payload: payload, var payload = $('#m').val()
from: 'SIN_A', if (payload.length === 0) {
to: 'SIN_B' return false;
}
var other = $('#other').val();
var otherSIN = new SIN(other);
var data = {
payload: payload,
from: sin,
to: other
}
socket.emit('message', data);
data.from = 'You';
show(data);
$('#m').val('');
return false;
});
// receive chat handler
socket.emit('subscribe', sin);
socket.on('message', function(msg)
{
show(msg);
}); });
$('#m').val('');
return false;
});
socket.emit('subscribe', 'SIN_B');
socket.on('SIN_A', function(msg)
{
$('#messages').append($('<li>').text(msg.ts+':'+JSON.stringify(msg.payload)));
}); });
</script> </script>
</body> </body>