forked from x/ContextOS
vps mod AppendJson
This commit is contained in:
parent
7ddbee3fd5
commit
548cfe512e
@ -1,6 +1,6 @@
|
|||||||
package tcp
|
package tcp // {{{
|
||||||
|
// }}}
|
||||||
import (
|
import ( // {{{
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
@ -9,13 +9,15 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
|
||||||
type TCP struct {
|
type TCP struct {
|
||||||
net.Conn
|
net.Conn
|
||||||
net.Listener
|
net.Listener
|
||||||
*ctx.Context
|
*ctx.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
|
||||||
c.Caches = map[string]*ctx.Cache{
|
c.Caches = map[string]*ctx.Cache{
|
||||||
"protocol": &ctx.Cache{Name: "网络协议(tcp/tcp4/tcp6)", Value: m.Conf("protocol"), Help: "网络协议"},
|
"protocol": &ctx.Cache{Name: "网络协议(tcp/tcp4/tcp6)", Value: m.Conf("protocol"), Help: "网络协议"},
|
||||||
"security": &ctx.Cache{Name: "加密通信(true/false)", Value: m.Conf("security"), Help: "加密通信"},
|
"security": &ctx.Cache{Name: "加密通信(true/false)", Value: m.Conf("security"), Help: "加密通信"},
|
||||||
@ -28,7 +30,8 @@ func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
// }}}
|
||||||
|
func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||||
tcp.Context.Master(nil)
|
tcp.Context.Master(nil)
|
||||||
if tcp.Context == Index {
|
if tcp.Context == Index {
|
||||||
Pulse = m
|
Pulse = m
|
||||||
@ -36,7 +39,8 @@ func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
|||||||
return tcp
|
return tcp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool {
|
// }}}
|
||||||
|
func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||||
if len(arg) > 1 {
|
if len(arg) > 1 {
|
||||||
m.Cap("address", arg[1])
|
m.Cap("address", arg[1])
|
||||||
}
|
}
|
||||||
@ -105,7 +109,8 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool {
|
// }}}
|
||||||
|
func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool { // {{{
|
||||||
switch tcp.Context {
|
switch tcp.Context {
|
||||||
case m.Target():
|
case m.Target():
|
||||||
if tcp.Listener != nil {
|
if tcp.Listener != nil {
|
||||||
@ -130,6 +135,8 @@ func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
|
||||||
var Pulse *ctx.Message
|
var Pulse *ctx.Message
|
||||||
var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
|
var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
|
||||||
Caches: map[string]*ctx.Cache{
|
Caches: map[string]*ctx.Cache{
|
||||||
@ -166,6 +173,18 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
|
|||||||
tcp.Conn.Read(buf)
|
tcp.Conn.Read(buf)
|
||||||
m.Echo(string(buf))
|
m.Echo(string(buf))
|
||||||
}},
|
}},
|
||||||
|
"ifconfig": &ctx.Command{Name: "ifconfig", Help: "接收消息",
|
||||||
|
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
|
ifs, e := net.Interfaces()
|
||||||
|
m.Assert(e)
|
||||||
|
for _, v := range ifs {
|
||||||
|
ips, e := v.Addrs()
|
||||||
|
m.Assert(e)
|
||||||
|
for _, x := range ips {
|
||||||
|
m.Echo("%d %s %v %v\n", v.Index, v.Name, v.HardwareAddr, x.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}},
|
||||||
},
|
},
|
||||||
Index: map[string]*ctx.Context{
|
Index: map[string]*ctx.Context{
|
||||||
"void": &ctx.Context{
|
"void": &ctx.Context{
|
||||||
|
@ -3,8 +3,10 @@ package web // {{{
|
|||||||
import ( // {{{
|
import ( // {{{
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"encoding/json"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -31,11 +33,15 @@ type WEB struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (web *WEB) AppendJson(msg *ctx.Message) string { // {{{
|
func (web *WEB) AppendJson(msg *ctx.Message) string { // {{{
|
||||||
|
b, e := json.Marshal(msg.Meta)
|
||||||
|
msg.Assert(e)
|
||||||
|
return string(b)
|
||||||
|
|
||||||
result := []string{"{"}
|
result := []string{"{"}
|
||||||
for i, k := range msg.Meta["append"] {
|
for i, k := range msg.Meta["append"] {
|
||||||
result = append(result, fmt.Sprintf("\"%s\": [", k))
|
result = append(result, fmt.Sprintf("\"%s\": [", k))
|
||||||
for j, v := range msg.Meta[k] {
|
for j, v := range msg.Meta[k] {
|
||||||
result = append(result, fmt.Sprintf("\"%s\"", v))
|
result = append(result, fmt.Sprintf("\"%s\"", url.QueryEscape(v)))
|
||||||
if j < len(msg.Meta[k])-1 {
|
if j < len(msg.Meta[k])-1 {
|
||||||
result = append(result, ",")
|
result = append(result, ",")
|
||||||
}
|
}
|
||||||
@ -46,6 +52,7 @@ func (web *WEB) AppendJson(msg *ctx.Message) string { // {{{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = append(result, "}")
|
result = append(result, "}")
|
||||||
|
|
||||||
return strings.Join(result, "")
|
return strings.Join(result, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user