123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package rest_websocket
- import (
- "log"
- "net/http"
- "git.ali33.ru/fcg-xvii/rest"
- "github.com/gorilla/websocket"
- )
- func New(app rest.IApplicationStream, core any) *Rest {
- return &Rest{
- upgrader: &websocket.Upgrader{
- ReadBufferSize: 1024,
- WriteBufferSize: 1024,
- CheckOrigin: func(r *http.Request) bool {
- // Проверьте ориджин здесь, и верните true, если он разрешен.
- //return r.Header.Get("Origin") == "http://example.com"
- return true
- },
- },
- appConf: &appConfig{
- app: app,
- core: core,
- },
- }
- }
- type Rest struct {
- upgrader *websocket.Upgrader
- appConf *appConfig
- server rest.IServer
- }
- func (s *Rest) Prepare(server rest.IServer, httpPrefix string) {
- s.appConf.ctx = server.Context()
- s.appConf.tokenGenerator = server.TokenGenerate
- s.appConf.tokenParser = server.TokenParse
- server.HandleFunc(httpPrefix, s.handle)
- s.server = server
- }
- func (s *Rest) handle(w http.ResponseWriter, r *http.Request) {
- // Преобразование HTTP соединения в WebSocket соединение
- ws, err := s.upgrader.Upgrade(w, r, nil)
- if err != nil {
- log.Println(err)
- }
- socket := NewSocketServer(
- NewSocket(ws, true),
- s.appConf,
- )
- s.appConf.app.Connect() <- socket
- }
|