1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- 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
- 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),
- s.appConf,
- )
- s.appConf.app.Connect() <- socket
- }
|