Skip to content

Instantly share code, notes, and snippets.

@balthazar
Created March 14, 2016 19:10
Show Gist options
  • Save balthazar/7eb5852845e3291f6958 to your computer and use it in GitHub Desktop.
Save balthazar/7eb5852845e3291f6958 to your computer and use it in GitHub Desktop.
minus sockets
diff --git a/src/api/socket.js b/src/api/socket.js
new file mode 100644
index 0000000..8fb6278
--- /dev/null
+++ b/src/api/socket.js
@@ -0,0 +1,24 @@
+import socketIo from 'socket.io'
+
+import config from 'config'
+
+class SocketServer {
+
+ constructor () {
+ this._io = socketIo({ serveClient: false })
+ this._io.listen(config.socketPort)
+ }
+
+}
+
+let io = null
+
+export const initSocketServer = () => {
+ io = new SocketServer()
+}
+
+export const getSocketServer = () => {
+ return io
+}
+
+export default getSocketServer()
diff --git a/src/components/App.js b/src/components/App.js
index 4e32534..cad1522 100644
--- a/src/components/App.js
+++ b/src/components/App.js
@@ -1,9 +1,17 @@
import React, { Component } from 'react'
+import io from 'socket.io-client'
+
+import config from 'config'
if (process.env.BROWSER) { require('styles/main.scss') }
class App extends Component {
+ componentWillMount () {
+ const socket = io.connect(`:${config.socketPort}`)
+ socket.on('connect', () => {})
+ }
+
render () {
return (
<div>
diff --git a/src/config/index.js b/src/config/index.js
index ee5024c..e6d468b 100644
--- a/src/config/index.js
+++ b/src/config/index.js
@@ -7,6 +7,7 @@ export default {
env,
port: 3000,
+ socketPort: 3002,
assetsFolder: path.join(__dirname, '../assets'),
distFolder: path.join(__dirname, '../../dist'),
diff --git a/src/dev/api.js b/src/dev/api.js
index a71f63c..13aa2cc 100644
--- a/src/dev/api.js
+++ b/src/dev/api.js
@@ -2,6 +2,7 @@ import express from 'express'
import bodyParser from 'body-parser'
import api from 'api'
+import { initSocketServer } from 'api/socket'
import config from 'config'
const server = express()
@@ -13,6 +14,8 @@ server.use((req, res, next) => {
next()
})
+initSocketServer()
+
server.use(bodyParser.json())
server.use(config.apiUrl, api)
diff --git a/src/server.js b/src/server.js
index 4fd038c..5252f52 100644
--- a/src/server.js
+++ b/src/server.js
@@ -4,6 +4,7 @@ import bodyParser from 'body-parser'
import config from 'config'
import api from 'api'
+import { initSocketServer } from 'api/socket'
import render from 'middlewares/render'
const server = express()
@@ -13,6 +14,7 @@ if (config.env === 'development') {
}
if (config.env === 'production') {
+ initSocketServer()
server.use(compression())
server.use(bodyParser.json())
server.use('/dist', express.static(config.distFolder))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment