-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwork.js
More file actions
84 lines (75 loc) · 1.43 KB
/
work.js
File metadata and controls
84 lines (75 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const _=require('underscore')
const glob=require('glob')
const base64Img = require('base64-img')
const os = require('os')
var wsarr=[]
var idx=0
var pics=[]
let basedir
if (os.platform()==='android'){
basedir="/home/local/web/"
}else{
basedir=""
}
glob(basedir+'public/images/*.jpeg',(err,files)=>{
if (!err) {
pics=files
}
})
function ws_count(){
return wsarr.length
}
function add_ws(ws){
if (_.findIndex(wsarr,(e)=>{
return ws===e
} )<0){
wsarr.push(ws)
}
}
function del_ws(ws){
wsarr=_.without(wsarr,ws)
}
function send_pic(ws){
if (os.platform()==='android'){
let picstr=android_fetch_pic()
if (picstr){
ws.call('update_pic','data:image/jpg;base64,'+picstr,function(err,ret){
if (err){
console.log("error occurs at browser:"+err)
}
})
}
}else{
if (idx>=pics.length){
return
}
let fn=pics[idx]
base64Img.base64(fn, (err, data) => {
if (err) {
console.log("loading gz.jpeg error:", err)
} else {
if (typeof ws.call ==='function') {
ws.call('update_pic',data,function(err,ret){})
}else{
console.error(ws)
}
}
})
}
}
function broadcast_pic(){
_.each(wsarr,(ws)=>{
send_pic(ws)
})
idx++
if (idx>=pics.length){
idx=0
}
}
module.exports={
add_ws,
del_ws,
send_pic,
broadcast_pic,
ws_count
}