This commit is contained in:
crusader 2017-08-25 19:41:43 +09:00
parent ba29302c19
commit f8655e116c
4 changed files with 135 additions and 20 deletions

49
handler/file/file.go Normal file
View File

@ -0,0 +1,49 @@
package file
import (
"log"
"git.loafle.net/overflow/overflow_gateway_web/handler"
gws "git.loafle.net/overflow/overflow_gateway_websocket"
"git.loafle.net/overflow/overflow_gateway_websocket/protocol/jsonrpc"
)
type FileHandler interface {
handler.Handler
}
type fileHandler struct {
co *gws.SocketOptions
ho *jsonrpc.Options
handler gws.MessageHandler
}
func New() FileHandler {
h := &fileHandler{}
h.ho = &jsonrpc.Options{
OnRequest: h.onRequest,
OnNotify: h.onNotify,
}
h.handler = jsonrpc.NewHandler(h.ho)
h.co = &gws.SocketOptions{
Handler: h.handler,
}
return h
}
func (h *fileHandler) GetSocketOption() *gws.SocketOptions {
return h.co
}
func (h *fileHandler) onRequest(soc gws.Socket, method string, params interface{}) (interface{}, error) {
log.Printf("path: %s, m: %s, params: %v", soc.Path(), method, params)
return nil, nil
}
func (h *fileHandler) onNotify(soc gws.Socket, method string, params interface{}) error {
return nil
}

9
handler/handler.go Normal file
View File

@ -0,0 +1,9 @@
package handler
import (
gws "git.loafle.net/overflow/overflow_gateway_websocket"
)
type Handler interface {
GetSocketOption() *gws.SocketOptions
}

50
handler/web/web.go Normal file
View File

@ -0,0 +1,50 @@
package web
import (
"log"
"git.loafle.net/overflow/overflow_gateway_web/handler"
gws "git.loafle.net/overflow/overflow_gateway_websocket"
"git.loafle.net/overflow/overflow_gateway_websocket/protocol/jsonrpc"
)
type WebHandler interface {
handler.Handler
}
type webHandler struct {
co *gws.SocketOptions
ho *jsonrpc.Options
handler gws.MessageHandler
}
func New() WebHandler {
h := &webHandler{}
h.ho = &jsonrpc.Options{
OnRequest: h.onRequest,
OnNotify: h.onNotify,
}
h.handler = jsonrpc.NewHandler(h.ho)
h.co = &gws.SocketOptions{
Handler: h.handler,
}
return h
}
func (h *webHandler) GetSocketOption() *gws.SocketOptions {
return h.co
}
func (h *webHandler) onRequest(soc gws.Socket, method string, params interface{}) (interface{}, error) {
log.Printf("path: %s, m: %s, params: %v", soc.Path(), method, params)
return nil, nil
}
func (h *webHandler) onNotify(soc gws.Socket, method string, params interface{}) error {
log.Printf("path: %s, m: %s, params: %v", soc.Path(), method, params)
return nil
}

47
main.go
View File

@ -1,40 +1,47 @@
package main
import (
"git.loafle.net/overflow/overflow_gateway_websocket"
"git.loafle.net/overflow/overflow_gateway_websocket/clients"
"log"
"github.com/valyala/fasthttp"
"git.loafle.net/overflow/overflow_gateway_web/handler/file"
"git.loafle.net/overflow/overflow_gateway_web/handler/web"
gws "git.loafle.net/overflow/overflow_gateway_websocket"
)
func main() {
o := &overflow_gateway_websocket.Options{
o := &gws.ServerOptions{
OnConnection: onConnection,
OnDisconnected: onDisconnected,
OnCheckOrigin: onCheckOrigin,
}
s := overflow_gateway_websocket.NewServer(o)
s := gws.NewServer(o)
co := &clients.Options{
OnRequest: onRequest,
OnNotify: onNotify,
}
wh := web.New()
fh := file.New()
s.HandleClient("/web", co)
s.HandleSocket("/web", wh.GetSocketOption())
s.HandleSocket("/file", fh.GetSocketOption())
s.ListenAndServe(":19090")
}
func onConnection(path string, c clients.Client) {
func onCheckOrigin(ctx *fasthttp.RequestCtx) bool {
if origin := string(ctx.Request.Header.Peek("Origin")); origin != "" {
ctx.Response.Header.Set("Access-Control-Allow-Origin", origin)
if string(ctx.Method()) == "OPTIONS" && string(ctx.Request.Header.Peek("Access-Control-Request-Method")) != "" {
ctx.Response.Header.Set("Access-Control-Allow-Headers", "Content-Type, Accept")
ctx.Response.Header.Set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE")
}
}
return true
}
func onDisconnected(c clients.Client) {
func onConnection(soc gws.Socket) {
log.Printf("connect: path: %s, id:%s\n", soc.Path(), soc.ID())
}
func onRequest(c clients.Client, method string, params interface{}) (interface{}, error) {
return nil, nil
}
func onNotify(c clients.Client, method string, params interface{}) error {
return nil
func onDisconnected(soc gws.Socket) {
log.Printf("disconnect: path: %s, id:%s\n", soc.Path(), soc.ID())
}