代码拉取完成,页面将自动刷新
同步操作将从 Hprose/hprose-go 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* hprose/websocket_service.go *
* *
* hprose websocket service for Go. *
* *
* LastModified: May 22, 2015 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package hprose
import (
"net/http"
"reflect"
"strings"
"sync"
"github.com/gorilla/websocket"
)
// WebSocketContext is the hprose websocket context
type WebSocketContext struct {
*HttpContext
WebSocket *websocket.Conn
}
// WebSocketService is the hprose websocket service
type WebSocketService struct {
*HttpService
*websocket.Upgrader
}
type wsArgsFixer struct {
httpArgsFixer
}
func (fixer wsArgsFixer) FixArgs(args []reflect.Value, lastParamType reflect.Type, context Context) []reflect.Value {
if c, ok := context.(*WebSocketContext); ok {
if lastParamType.String() == "*hprose.WebSocketContext" {
return append(args, reflect.ValueOf(c))
} else if lastParamType.String() == "*websocket.Conn" {
return append(args, reflect.ValueOf(c.WebSocket))
} else if lastParamType.String() == "*hprose.HttpContext" {
return append(args, reflect.ValueOf(c.HttpContext))
} else if lastParamType.String() == "*http.Request" {
return append(args, reflect.ValueOf(c.Request))
}
}
return fixer.httpArgsFixer.FixArgs(args, lastParamType, context)
}
// NewWebSocketService is the constructor of WebSocketService
func NewWebSocketService() *WebSocketService {
service := new(WebSocketService)
service.HttpService = NewHttpService()
service.argsfixer = wsArgsFixer{}
service.Upgrader = &websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
origin := r.Header.Get("origin")
if origin != "" && origin != "null" {
if len(service.accessControlAllowOrigins) == 0 || service.accessControlAllowOrigins[origin] {
return true
}
return false
}
return true
},
}
return service
}
// ServeHTTP ...
func (service *WebSocketService) ServeHTTP(response http.ResponseWriter, request *http.Request) {
if request.Method == "GET" && strings.ToLower(request.Header.Get("connection")) != "upgrade" || request.Method == "POST" {
service.HttpService.ServeHTTP(response, request)
return
}
conn, err := service.Upgrade(response, request, nil)
if err != nil {
context := new(HttpContext)
context.BaseContext = NewBaseContext()
context.Response = response
context.Request = request
service.fireErrorEvent(err, context)
return
}
defer conn.Close()
mutex := sync.Mutex{}
for {
context := new(WebSocketContext)
context.HttpContext = new(HttpContext)
context.BaseContext = NewBaseContext()
context.Response = response
context.Request = request
context.WebSocket = conn
msgType, data, err := conn.ReadMessage()
if err != nil {
break
}
if msgType == websocket.BinaryMessage {
go func(conn *websocket.Conn, data []byte, context *WebSocketContext) {
id := data[0:4]
data = service.Handle(data[4:], context)
msg := make([]byte, len(data)+4)
copy(msg, id)
copy(msg[4:], data)
err := func() error {
mutex.Lock()
defer mutex.Unlock()
return conn.WriteMessage(websocket.BinaryMessage, msg)
}()
if err != nil {
service.fireErrorEvent(err, context)
conn.Close()
}
}(conn, data, context)
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。