Skip to content

Commit dfa0d7c

Browse files
committed
incremenet
1 parent 40d2c09 commit dfa0d7c

10 files changed

Lines changed: 162 additions & 26 deletions

File tree

client/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
/node_modules/
1+
/node_modules/
2+
.cache
3+
dist

client/OBScore.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { emittable, receivable } from "./events"
2-
import * as socketio from "socket.io"
3-
4-
declare global {
5-
interface Window {io: Function}
6-
}
2+
import Scoreboard from "../controller/src/model/Scoreboard"
3+
import io from "socket.io-client"
74

85
export default class OBS {
96
private css: string = `
@@ -14,11 +11,15 @@ export default class OBS {
1411

1512
private name: string
1613
private timeout: number
17-
private onUpdate: Function
18-
private socket?: socketio.Server
14+
private onUpdate: (scoreboard: Scoreboard) => void
15+
private socket?: SocketIOClient.Socket
1916
private printedNotReady: boolean = true
2017

21-
constructor(name: string, onUpdate: Function, timeout = 500) {
18+
constructor(
19+
name: string,
20+
onUpdate: (scoreboard: Scoreboard) => void,
21+
timeout = 500
22+
) {
2223
if (typeof name !== "string")
2324
throw new Error("name must be a string")
2425

@@ -48,34 +49,34 @@ export default class OBS {
4849
sendError(error: Error) {
4950
if (typeof this.socket !== "object") {
5051
throw new Error("Socket not available yet")
51-
return
5252
}
5353

5454
this.socket.emit(emittable.clientError, error.stack)
5555
}
5656

57-
connect(server: string) {
58-
if (typeof window.io !== "function")
59-
throw new Error("socket.io not found")
60-
61-
/* global io */
62-
const socket = window.io(server)
57+
connect(server: string, callback?: Function) {
58+
const socket = io(server)
6359
this.socket = socket
6460

65-
socket.on(receivable.connect, () => {
61+
socket.on(receivable.connect, (anything: any) => {
62+
console.log(anything)
6663
this.log("Connected to OBScore-Host")
6764
socket.emit(emittable.introduce, this.name)
6865
socket.emit(emittable.gibData)
66+
67+
if (callback) {
68+
callback()
69+
}
6970
})
7071

7172
socket.on(receivable.disconnect, () =>
7273
this.logError("OBScore-Host Disconnected")
7374
)
7475

75-
socket.on(receivable.update, (data: Object) => {
76+
socket.on(receivable.update, (scoreboard: Scoreboard) => {
7677
this.log("Received update:")
77-
console.log(data)
78-
this.onUpdate(data)
78+
console.log(scoreboard)
79+
this.onUpdate(scoreboard)
7980
this.printedNotReady = false
8081
})
8182

client/package-lock.json

Lines changed: 103 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "client",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "waffeln",
10+
"license": "ISC",
11+
"dependencies": {
12+
"@types/socket.io": "^2.1.4",
13+
"@types/socket.io-client": "^1.4.32",
14+
"socket.io": "^2.3.0"
15+
}
16+
}

client/sample-receive/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
<title>Document</title>
88
</head>
99
<body>
10-
<script src=""></script>
10+
<script src="./main.ts"></script>
1111
</body>
1212
</html>

client/sample-receive/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import OBS from "../OBScore"
2+
3+
const obs = new OBS("something", data => {
4+
console.log(data)
5+
})
6+
7+
obs.connect("ws://localhost:3001")

client/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
/* Basic Options */
44
// "incremental": true, /* Enable incremental compilation */
5-
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
5+
"target": "ES6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
66
"module": "none", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
77
// "lib": [], /* Specify library files to be included in the compilation. */
88
"allowJs": true, /* Allow javascript files to be compiled. */

host/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ ioSocket.on(receivable.global.connect, socket => {
3333
changeConfig(data, config)
3434
)
3535

36-
socket.on(receivable.global.introduce, name =>
36+
socket.on(receivable.global.introduce, (name: string) => {
3737
nameTable[socket.id] = name
38-
)
38+
logDefault(`Client ${formatId(socket.id)} is now known as ${formatName(name)}`)
39+
})
3940

4041
socket.on(receivable.global.logDefault, msg =>
4142
socketLogDefault(msg, getName, socket)

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"homepage": "https://github.com/waffln/obscore-react#readme",
1919
"devDependencies": {
20-
"eslint-plugin-react": "^7.18.0"
20+
"eslint-plugin-react": "^7.18.0",
21+
"typescript": "^3.7.5"
2122
}
2223
}

0 commit comments

Comments
 (0)