5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 06:51:26 +08:00
wails/lib/renderer/bridge/session.go
Lea Anthony a2f16aba9b
Feature/1.16 (#646)
* Initial support for embed

* Security (#644)

* Bump y18n from 3.2.1 to 3.2.2 in /runtime/js/runtime (#639)

* Create FUNDING.yml

* Update README.md

* Bump y18n from 3.2.1 to 3.2.2 in /runtime/js/runtime

Bumps [y18n](https://github.com/yargs/y18n) from 3.2.1 to 3.2.2.
- [Release notes](https://github.com/yargs/y18n/releases)
- [Changelog](https://github.com/yargs/y18n/blob/master/CHANGELOG.md)
- [Commits](https://github.com/yargs/y18n/commits)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump elliptic from 6.5.3 to 6.5.4 in /runtime/js (#617)

Bumps [elliptic](https://github.com/indutny/elliptic) from 6.5.3 to 6.5.4.
- [Release notes](https://github.com/indutny/elliptic/releases)
- [Commits](https://github.com/indutny/elliptic/compare/v6.5.3...v6.5.4)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump y18n from 4.0.0 to 4.0.1 in /runtime/js (#643)

* Create FUNDING.yml

* Update README.md

* Updated sponsors

* Consistent styling of README.md

* Bump y18n from 4.0.0 to 4.0.1 in /runtime/js (#638)

Bumps [y18n](https://github.com/yargs/y18n) from 4.0.0 to 4.0.1.
- [Release notes](https://github.com/yargs/y18n/releases)
- [Changelog](https://github.com/yargs/y18n/blob/master/CHANGELOG.md)
- [Commits](https://github.com/yargs/y18n/commits)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Revert "Bump y18n from 4.0.0 to 4.0.1 in /runtime/js (#638)" (#642)

This reverts commit 17b28a26bd.

* Bump y18n from 4.0.0 to 4.0.1 in /runtime/js

Bumps [y18n](https://github.com/yargs/y18n) from 4.0.0 to 4.0.1.
- [Release notes](https://github.com/yargs/y18n/releases)
- [Changelog](https://github.com/yargs/y18n/blob/master/CHANGELOG.md)
- [Commits](https://github.com/yargs/y18n/commits)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Support for minimum and maximum window sizes (#612)

* add support for minimum and maximum window sizes

* attempt to fix windows

* bug fixes

* support min/max window sizes on Linux and Windows

* fix min/max window sizes on Linux

* formatting and comments

* fixes Windows DPI issue, clamps width/height values to min/max

* App can't go into full screen when max size is set for Mac

* fixed Linux maximum width/height on window maximize

* Revert "fixed Linux maximum width/height on window maximize"

This reverts commit 3f7ba8b264.

The fix glitches on PopOS

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>

* v1.13.0

* Remove mewn. Update templates. Remove logging.

* Import embed in templates!

* Fix react import path

* Update README.md

* Updated sponsors

* Consistent styling of README.md

* Revert "Bump y18n from 4.0.0 to 4.0.1 in /runtime/js (#638)" (#642)

This reverts commit 17b28a26bd.

* Update actions to use 1.16

* v1.16.0

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: RH12503 <48951973+RH12503@users.noreply.github.com>
2021-04-02 20:44:55 +11:00

136 lines
3.1 KiB
Go

package renderer
import (
"time"
"github.com/wailsapp/wails/runtime"
"github.com/gorilla/websocket"
"github.com/wailsapp/wails/lib/interfaces"
"github.com/wailsapp/wails/lib/logger"
)
// TODO Move this back into bridge.go
// session represents a single websocket session
type session struct {
bindingCache []string
conn *websocket.Conn
eventManager interfaces.EventManager
log *logger.CustomLogger
ipc interfaces.IPCManager
// Mutex for writing to the socket
shutdown chan bool
writeChan chan []byte
done bool
}
func newSession(conn *websocket.Conn, bindingCache []string, ipc interfaces.IPCManager, logger *logger.CustomLogger, eventMgr interfaces.EventManager) *session {
return &session{
conn: conn,
bindingCache: bindingCache,
ipc: ipc,
log: logger,
eventManager: eventMgr,
shutdown: make(chan bool),
writeChan: make(chan []byte, 100),
}
}
// Identifier returns a string identifier for the remote connection.
// Taking the form of the client's <ip address>:<port>.
func (s *session) Identifier() string {
if s.conn != nil {
return s.conn.RemoteAddr().String()
}
return ""
}
func (s *session) sendMessage(msg string) error {
if !s.done {
s.writeChan <- []byte(msg)
}
return nil
}
func (s *session) start(firstSession bool) {
s.log.Infof("Connected to frontend.")
go s.writePump()
s.evalJS(runtime.WailsJS, wailsRuntimeMessage)
// Inject bindings
for _, binding := range s.bindingCache {
s.evalJS(binding, bindingMessage)
}
s.eventManager.Emit("wails:bridge:session:started", s.Identifier())
// Emit that everything is loaded and ready
if firstSession {
s.eventManager.Emit("wails:ready")
}
for {
messageType, buffer, err := s.conn.ReadMessage()
if messageType == -1 {
return
}
if err != nil {
s.log.Errorf("Error reading message: %v", err)
continue
}
s.log.Debugf("Got message: %#v\n", string(buffer))
s.ipc.Dispatch(string(buffer), s.Callback)
if s.done {
break
}
}
}
// Callback sends a callback to the frontend
func (s *session) Callback(data string) error {
return s.evalJS(data, callbackMessage)
}
func (s *session) evalJS(js string, mtype messageType) error {
// Prepend message type to message
return s.sendMessage(mtype.toString() + js)
}
// Shutdown
func (s *session) Shutdown() {
s.done = true
s.shutdown <- true
s.log.Debugf("session %v exit", s.Identifier())
}
// writePump pulls messages from the writeChan and sends them to the client
// since it uses a channel to read the messages the socket is protected without locks
func (s *session) writePump() {
s.log.Debugf("Session %v - writePump start", s.Identifier())
for {
select {
case msg, ok := <-s.writeChan:
s.conn.SetWriteDeadline(time.Now().Add(1 * time.Second))
if !ok {
s.log.Debug("writeChan was closed!")
s.conn.WriteMessage(websocket.CloseMessage, []byte{})
return
}
if err := s.conn.WriteMessage(websocket.TextMessage, msg); err != nil {
s.log.Debug(err.Error())
return
}
case <-s.shutdown:
break
}
}
s.log.Debug("writePump exiting...")
}