Skip to content

Commit

Permalink
#add acknowledgement callback
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstyles committed Dec 7, 2015
1 parent daa10a4 commit bffd585
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
18 changes: 17 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
}

button {
display: block;
margin-top: .5em;
border: none;
border-radius: 4px;
font-size: 18px;
Expand All @@ -31,6 +33,9 @@
background: #36F1CD;
text-shadow: 0px 1px 2px rgba( 0, 0, 0, .3 );
}
.small {
font-size: 70%;
}

.connections {
position: absolute;
Expand All @@ -53,18 +58,29 @@
</head>
<body>
<button class="js-btn">Send</button>
<button class="js-ack">Send <span class="small">(with acknowledgement)</span></button>
<button class="js-chatBtn">Chat</button>
<div class="connections"><span class="js-connect">1</span></div>
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
<script>
var btn = document.querySelector( '.js-btn' )
var socket = io()

var btn = document.querySelector( '.js-btn' )
btn.addEventListener( 'click', function( event ) {
socket.emit( 'data', {
foo: 'foo'
})
})

var ack = document.querySelector( '.js-ack' )
ack.addEventListener( 'click', function( event ) {
socket.emit( 'ack', {
foo: 'foo'
}, res => {
console.log( 'acknowledgement:', res )
})
})

var connections = document.querySelector( '.js-connect' )
socket.on( 'connections', function( event ) {
console.log( 'connected', event )
Expand Down
4 changes: 4 additions & 0 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ socket.on( 'data', ( ctx, data ) => {
message: 'response from server'
})
})
socket.on( 'ack', ( ctx, data ) => {
console.log( 'data event with acknowledgement', data )
ctx.acknowledge( 'received' )
})
socket.on( 'numConnections', packet => {
console.log( `Number of connections: ${ socket.connections.size }` )
})
Expand Down
5 changes: 3 additions & 2 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ module.exports = class Socket {
* @param data <?>
*/
on( event, handler ) {
this.socket.on( event, data => {
this.socket.on( event, ( data, cb ) => {
let packet = {
event: event,
data: data,
socket: this
socket: this,
acknowledge: cb
}

if ( !this.middleware ) {
Expand Down

0 comments on commit bffd585

Please sign in to comment.