forked from x/volcanos
opt socket
This commit is contained in:
parent
efee943911
commit
ec57c4ee10
22
lib/misc.js
22
lib/misc.js
@ -64,13 +64,25 @@ Volcanos("misc", {help: "工具模块",
|
||||
}), delete(event.msg)
|
||||
}),
|
||||
WSS: shy("请求后端", {order: 0}, function(can, url, args, cb, onopen, onerror, onclose) {var meta = arguments.callee.meta
|
||||
if (url.indexOf("ws") == -1) {
|
||||
url = location.protocol.replace("http", "ws")+"//"+location.host+"/space/" + (url||"")
|
||||
}
|
||||
|
||||
if (can._socket) {return can._socket}
|
||||
args["share"] = can._share || ""
|
||||
can._socket = new WebSocket(url+"?"+can.base.Args(args))
|
||||
|
||||
can._socket.onclose = onclose || function() {
|
||||
can._socket.onclose = onclose || function() {if (!can._socket) {return}
|
||||
console.log("socket close")
|
||||
|
||||
}, can._socket.onerror = onerror || function() {
|
||||
can.user.toast("wss redial")
|
||||
can._socket.close(), can.Log("wss", "close"), delete(can._socket), setTimeout(function() {
|
||||
delete(can._socket), setTimeout(function() {
|
||||
// 断线重连
|
||||
can.misc.WSS(can, url, args, cb, onerror, onclose, onopen)
|
||||
}, 1000)
|
||||
}, can._socket.onerror = onerror || function() {if (!can._socket) {return}
|
||||
console.log("socket error")
|
||||
|
||||
can._socket.close(), delete(can._socket), setTimeout(function() {
|
||||
// 断线重连
|
||||
can.misc.WSS(can, url, args, cb, onerror, onclose, onopen)
|
||||
}, 1000)
|
||||
@ -97,7 +109,7 @@ Volcanos("misc", {help: "工具模块",
|
||||
try {
|
||||
// 执行命令
|
||||
can.Log(["wss", order].concat(msg.detail).concat([msg]))
|
||||
typeof cb == "function" && cb(event, msg)
|
||||
typeof cb == "function" && cb(event, msg, msg.detail[0], msg.detail.slice(1))
|
||||
} catch (e) {
|
||||
// 执行失败
|
||||
msg.Reply(can.Log("err", e))
|
||||
|
@ -1,6 +1,17 @@
|
||||
Volcanos("onimport", {help: "导入数据", list: [],
|
||||
_init: function(can, conf, output, action, option, field) {
|
||||
can.user.login = function(cb) {
|
||||
can.misc.WSS(can, "", {node: "active"}, function(event, msg, cmd, arg) {
|
||||
switch (cmd) {
|
||||
case "space":
|
||||
can._share = arg[1]
|
||||
break
|
||||
case "sessid":
|
||||
can.user.Cookie(can, "sessid", arg[0]), can.user.toast(""), can.Hide()
|
||||
typeof cb == "function" && cb({name: msg["user.name"]})
|
||||
break
|
||||
}
|
||||
})
|
||||
can.user.Cookie("sessid")? can.onaction.check(event, can, cb, "check", output):
|
||||
can.onaction.login(event, can, cb, "login", output)
|
||||
}
|
||||
@ -13,7 +24,7 @@ Volcanos("onimport", {help: "导入数据", list: [],
|
||||
}
|
||||
can.run(event, ["share", value, msg.Option("name"), msg.Option("text")].concat(list), function(msg) {
|
||||
var p = "/share/" + msg.Result(); can.user.toast({title: msg.Option("name"),
|
||||
text: [{text: can.user.Share(can, {path: p}, true)}, {img: p+"/qrcode"}],
|
||||
text: [{text: '<a target="_blank" href="'+can.user.Share(can, {path: p}, true)+'">'+p+'</a>'}, {img: p+"/share"}],
|
||||
width: 300, height: 400, duration: 300000,
|
||||
})
|
||||
})
|
||||
@ -42,40 +53,14 @@ Volcanos("onaction", {help: "组件交互", list: [],
|
||||
})
|
||||
}]},
|
||||
{button: ["扫码登录", function(event, cmd) {
|
||||
can.onaction.socket(event, can, "", function(event, msg) {
|
||||
switch (msg.detail[0]) {
|
||||
case "space":
|
||||
if (msg.detail[1] == "share") {can._share = msg.detail[2]
|
||||
can.user.toast({title: "请用微信扫描", list: [{img: [can.user.Share(can, {
|
||||
path: "/share/"+msg.detail[2],
|
||||
}, true)]}]})
|
||||
return true
|
||||
}
|
||||
break
|
||||
case "sessid":
|
||||
can.user.Cookie(can, "sessid", msg.detail[1])
|
||||
can.Hide(), typeof cb == "function" && cb({name: msg["user.name"]})
|
||||
can.user.toast("")
|
||||
return true
|
||||
}
|
||||
})
|
||||
can.user.toast({title: "请用微信扫码("+can._share+")", list: [{img: [can.user.Share(can, {
|
||||
path: "/share/"+can._share+"/value",
|
||||
}, true)]}]})
|
||||
}]},
|
||||
{type: "br"},
|
||||
])
|
||||
can.Show(event, -1, -1)
|
||||
},
|
||||
socket: function(event, can, value, cmd, output) {can._username = value
|
||||
return can._socket = can._socket || can.misc.WSS(can, "wss://shylinux.com/space/", {node: "active", name: ""}, function(event, msg) {
|
||||
if (msg.Option("_handle")) {return can.user.toast(msg.result.join(""))}
|
||||
if (typeof cmd == "function" && cmd(event, msg)) {return msg.Reply(msg)}
|
||||
|
||||
switch (msg.detail[0]) {
|
||||
case "space": can._share = msg.detail[2]; break
|
||||
case "pwd": msg.Echo("hello world"); break
|
||||
}
|
||||
msg.Reply(msg)
|
||||
})
|
||||
},
|
||||
})
|
||||
Volcanos("onchoice", {help: "组件菜单", list: []})
|
||||
Volcanos("ondetail", {help: "组件详情", list: []})
|
||||
|
Loading…
x
Reference in New Issue
Block a user