Skip to content

Commit

Permalink
Setting up live tweet details over websockets.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Thomas committed Mar 16, 2016
1 parent bc24e75 commit 70f4b96
Show file tree
Hide file tree
Showing 13 changed files with 7,733 additions and 6 deletions.
8 changes: 5 additions & 3 deletions web/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict'
const express = require('express')
const app = express()
const http = require('http').Server(app)
const io = require('socket.io')(http)
const compression = require('compression')
const cfenv = require('cfenv')
const winston = require('winston')
Expand All @@ -15,11 +18,10 @@ if (!db_creds) {

const creds = {username: db_creds.username, password: db_creds.password}

const app = express()
app.use(express.static('public'))
app.use(compression())
routes(app, creds)
routes(app, io, creds)

app.listen(appEnv.port, () => {
http.listen(appEnv.port, () => {
winston.info(`Match Tracker app listening on port ${appEnv.port}.`)
})
3 changes: 3 additions & 0 deletions web/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ScrollingMatchEvents from './components/ScrollingMatchEvents'
import LoadingModal from './components/LoadingModal'
import Gameweek from './stores/Gameweek'
import GameWeekActions from './actions/GameWeekActions'
import WebSocket from './utils/WebSocket'

GameWeekActions.loadGameWeek(window.current_gameweek)

Expand All @@ -33,10 +34,12 @@ class MatchTracker extends React.Component {

componentDidMount () {
Gameweek.addChangeListener(this._onChange)
WebSocket.listen()
}

componentWillUnmount () {
Gameweek.removeChangeListener(this._onChange)
WebSocket.ignore()
}

render () {
Expand Down
29 changes: 29 additions & 0 deletions web/client/utils/WebSocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import io from 'socket.io-client'
class WebSocket {
constructor () {
this.socket = io()
this.topic = 'updates'
this.socket.on('connect', () => {
console.log('listening to updates');
})
this.socket.on('disconnect', () => {
console.log('disconnected from updates')
})
}

listen () {
this.socket.on(this.topic, this.listener)
}

ignore () {
this.socket.removeListener(this.listener)
}

listener (msg) {
console.log(msg)
}
}

const websocket = new WebSocket()

export default websocket
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"keymirror": "^0.1.1",
"moment": "^2.12.0",
"request": "^2.69.0",
"socket.io": "^1.4.5",
"superagent": "^1.6.1",
"winston": "^2.1.1"
},
Expand Down
Loading

0 comments on commit 70f4b96

Please sign in to comment.