Skip to content
This repository has been archived by the owner on Dec 2, 2019. It is now read-only.

Commit

Permalink
refractor
Browse files Browse the repository at this point in the history
  • Loading branch information
Dougley committed Feb 23, 2018
1 parent 1e6d9cf commit eb0f8f8
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 20 deletions.
39 changes: 39 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: 2
jobs:
build:
docker:
- image: docker:latest
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: true
- run:
name: Docker building
command: |
if [ "$CIRCLE_BRANCH" = "master" ]; then docker build -t dougley/bezerk:latest .; else docker build -t dougley/bezerk:$CIRCLE_BRANCH .; fi
docker login -u $DOCKER_USER -p $DOCKER_PASS
if [ "$CIRCLE_BRANCH" = "master" ]; then docker push dougley/bezerk:latest; else docker push dougley/bezerk:$CIRCLE_BRANCH; fi
tagged-build:
docker:
- image: docker:latest
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: true
- run:
name: Docker building
command: |
docker build -t dougley/bezerk:$(git describe --abbrev=0 --tags) .
docker login -u $DOCKER_USER -p $DOCKER_PASS
docker push dougley/bezerk:$(git describe --abbrev=0 --tags)
workflows:
version: 2
test_n_build:
jobs:
- build
- tagged-build:
filters:
branches:
ignore: /.*/
tags:
only: /v.+/
42 changes: 38 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,63 @@
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules
# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next

# idea files
.idea
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:9
MAINTAINER hello@dougley.com

WORKDIR /app

CMD ["node", "index.js"]

COPY package.json /app/

# Create working dir
RUN mkdir -p /app
COPY . /app

RUN npm i
38 changes: 22 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ const Server = new WSS({
port: process.env.BEZERK_PORT
})

const sockets = new Map()

Server.on('connection', socket => {
socket.on('message', msg => handle(socket, msg))
socket.on('close', () => {
console.log(Server.clients)
})
socket.on('error', console.error)
send(socket, {
op: '1001'
})
})

function handle (socket, msg) {
if (socket.readyState !== 1) return
try {
msg = JSON.parse(msg)
validate(socket, msg)
Expand All @@ -35,7 +38,7 @@ function handle (socket, msg) {
})
if (msg.c.shard) {
socket.type = 'shard'
sockets.set(`shard:${msg.c.shard}`, socket)
socket.shardid = msg.c.shard
} else {
socket.type = 'listener'
}
Expand Down Expand Up @@ -65,20 +68,21 @@ function handle (socket, msg) {
case '2005': { // REQUEST_APPLY
if (socket.type === 'listener') {
if (msg.c.shard !== undefined) {
if (sockets.has(`shard:${msg.c.shard}`)) {
send(sockets.get(`shard:${msg.c.shard}`), {
op: '2001', // REQUEST
c: msg.c
})
} else {
send(socket, {
op: '5000' // CANNOT_COMPLY
})
}
Server.clients.forEach(x => {
if (x.type === 'shard' && x.shardid) {
return send(x, {
op: '2001', // REQUEST
c: msg.c
})
}
})
send(socket, {
op: '5000' // CANNOT_COMPLY
})
} else {
sockets.forEach((value, key) => {
if (value.startsWith('shard:')) {
send(key, {
Server.clients.forEach(x => {
if (x.type === 'shard') {
send(x, {
op: '2001', // REQUEST
c: msg.c
})
Expand All @@ -92,11 +96,13 @@ function handle (socket, msg) {
}

function send (socket, payload) {
if (socket.readyState !== 1) return
if (typeof payload === 'object') payload = JSON.stringify(payload)
socket.send(payload)
}

function validate (socket, msg) {
if (socket.readyState !== 1) return
if (msg.op === undefined) throw new Error()
if (msg.c === undefined) throw new Error()
return true
Expand Down

0 comments on commit eb0f8f8

Please sign in to comment.