@symbo.ls/socket 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client.js +10 -3
- package/package.json +1 -1
- package/server.js +18 -9
package/client.js
CHANGED
|
@@ -2,10 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
import io from 'socket.io-client'
|
|
4
4
|
|
|
5
|
+
const url = 'ws://localhost:1001'
|
|
5
6
|
let socket
|
|
7
|
+
let appKkey
|
|
6
8
|
|
|
7
|
-
export const connect = (
|
|
9
|
+
export const connect = (key, receive) => {
|
|
8
10
|
socket = io(url)
|
|
11
|
+
appKkey = key
|
|
12
|
+
|
|
13
|
+
// console.log(socket)
|
|
14
|
+
// socket.join(key)
|
|
9
15
|
|
|
10
16
|
socket.on('connect', () => {
|
|
11
17
|
console.log(`event: connect | session id: ${socket.id}`)
|
|
@@ -20,6 +26,7 @@ export const connect = (url, receive) => {
|
|
|
20
26
|
})
|
|
21
27
|
|
|
22
28
|
socket.onAny((event, ...args) => {
|
|
29
|
+
console.log(event, args[0])
|
|
23
30
|
if (event === 'init') receive(event, args[0])
|
|
24
31
|
else {
|
|
25
32
|
try {
|
|
@@ -31,8 +38,8 @@ export const connect = (url, receive) => {
|
|
|
31
38
|
})
|
|
32
39
|
}
|
|
33
40
|
|
|
34
|
-
export const send = changes => {
|
|
35
|
-
socket.emit('change', changes)
|
|
41
|
+
export const send = (changes, options) => {
|
|
42
|
+
socket.emit('change', changes, options)
|
|
36
43
|
}
|
|
37
44
|
|
|
38
45
|
export const disconnect = changes => {
|
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -5,7 +5,7 @@ import express from 'express'
|
|
|
5
5
|
import http from 'http'
|
|
6
6
|
import { Server } from 'socket.io'
|
|
7
7
|
import { dirname } from 'path' // eslint-disable-line
|
|
8
|
-
import
|
|
8
|
+
import mergeDeep from 'merge-deep' // use the require method
|
|
9
9
|
|
|
10
10
|
import { createRequire } from 'module'
|
|
11
11
|
|
|
@@ -14,8 +14,9 @@ const DYNAMIC_CONFIG = require('@symbo.ls/init/src/dynamic.json') // Bring in th
|
|
|
14
14
|
|
|
15
15
|
const app = express()
|
|
16
16
|
|
|
17
|
-
export const sync = (symbolsInitFile = DYNAMIC_CONFIG) => {
|
|
17
|
+
export const sync = (symbolsInitFile = DYNAMIC_CONFIG, options = {}) => {
|
|
18
18
|
const server = http.createServer(app)
|
|
19
|
+
console.log(options)
|
|
19
20
|
|
|
20
21
|
const io = new Server(server, {
|
|
21
22
|
transports: ['websocket', 'polling', 'flashsocket'],
|
|
@@ -29,17 +30,25 @@ export const sync = (symbolsInitFile = DYNAMIC_CONFIG) => {
|
|
|
29
30
|
})
|
|
30
31
|
|
|
31
32
|
io.on('connection', (socket) => {
|
|
32
|
-
console.log('
|
|
33
|
+
console.log('Connected', options.key)
|
|
33
34
|
|
|
34
|
-
socket.
|
|
35
|
+
socket.join(options.key)
|
|
35
36
|
|
|
36
|
-
socket.
|
|
37
|
-
|
|
37
|
+
socket.to(options.key).emit('init', symbolsInitFile)
|
|
38
|
+
|
|
39
|
+
socket.on('change', changes => {
|
|
40
|
+
const file = require('@symbo.ls/init/src/dynamic.json')
|
|
41
|
+
const newMerge = mergeDeep(file, changes)
|
|
38
42
|
const mergeStr = JSON.stringify(newMerge, null, 2)
|
|
39
43
|
const initPath = process.cwd() + '/node_modules/@symbo.ls/init/src/dynamic.json'
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
44
|
+
|
|
45
|
+
if (!options.live) {
|
|
46
|
+
fs.writeFile(initPath, mergeStr, function (err) {
|
|
47
|
+
if (err) { return console.log(err) }
|
|
48
|
+
})
|
|
49
|
+
} else {
|
|
50
|
+
io.to(options.key).emit('live-change', mergeStr)
|
|
51
|
+
}
|
|
43
52
|
})
|
|
44
53
|
})
|
|
45
54
|
|