Skip to content

Commit

Permalink
#update basic example
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstyles committed Mar 29, 2016
1 parent 0487539 commit b9705de
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const IO = require( '../' )
const co = require( 'co' )

const app = new Koa()
const socket = new IO()
const io = new IO()
const chat = new IO( 'chat' )

socket.attach( app )
io.attach( app )
chat.attach( app )

/**
Expand All @@ -35,51 +35,51 @@ app.use( ctx => {
/**
* Socket middlewares
*/
socket.use( co.wrap( function *( ctx, next ) {
io.use( co.wrap( function *( ctx, next ) {
console.log( 'Socket middleware' )
const start = new Date
yield next()
const ms = new Date - start
console.log( `WS ${ ms }ms` )
}))
socket.use( co.wrap( function *( ctx, next ) {
io.use( co.wrap( function *( ctx, next ) {
ctx.teststring = 'test'
yield next()
}))

/**
* Socket handlers
*/
socket.on( 'connection', ctx => {
io.on( 'connection', ctx => {
console.log( 'Join event', ctx.socket.id )
socket.broadcast( 'connections', {
numConnections: socket.connections.size
io.broadcast( 'connections', {
numConnections: io.connections.size
})
// app.io.broadcast( 'connections', {
// numConnections: socket.connections.size
// })
})

socket.on( 'disconnect', ctx => {
io.on( 'disconnect', ctx => {
console.log( 'leave event', ctx.socket.id )
socket.broadcast( 'connections', {
numConnections: socket.connections.size
io.broadcast( 'connections', {
numConnections: io.connections.size
})
})
socket.on( 'data', ( ctx, data ) => {
io.on( 'data', ( ctx, data ) => {
console.log( 'data event', data )
console.log( 'ctx:', ctx.event, ctx.data, ctx.socket.id )
console.log( 'ctx.teststring:', ctx.teststring )
ctx.socket.emit( 'response', {
message: 'response from server'
})
})
socket.on( 'ack', ( ctx, data ) => {
io.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 }` )
io.on( 'numConnections', packet => {
console.log( `Number of connections: ${ io.connections.size }` )
})

/**
Expand All @@ -90,8 +90,15 @@ chat.on( 'connection', ctx => {
})
chat.on( 'message', ctx => {
console.log( 'chat message received', ctx.data )
// app.chat.broadcast( 'message', 'yo connections, lets chat' )
chat.broadcast( 'message', 'ok connections:chat' )

// Broadcasts to everybody, including this connection
app.chat.broadcast( 'message', 'yo connections, lets chat' )

// Broadcasts to all other connections
ctx.socket.broadcast( 'message', 'ok connections:chat:broadcast' )

// Emits to just this socket
ctx.socket.emit( 'message', 'ok connections:chat:emit' )
})

const PORT = 3000
Expand Down

0 comments on commit b9705de

Please sign in to comment.