5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 16:52:07 +08:00
wails/lib/renderer/bridge/session.go
Lea Anthony 0c2c56e1dd
v1.8.1 (#546)
* resolve angular routing broken when app is built

* Updated contributors

* handle Set error on Update method

* Remove incorrect build flag

* v1.8.1-pre1

* added Get method

* fixed return

* implement gopath handling during cross compilation

* update messages to output xgo version

TODO: allow image override

* use wailsapp/xgo:1.0.1 for cross-compiling

* Support platform list in templates

* Add warnings for windows builds

* add fields Tags to ProjectOptions

* add args tags to func BuildNative

* add tags to func BuildDocker

* delete escape tags

* Update Contributors

* feat: Vue3, Vue Router, Vuex, and Typescript Template

* converted spaces to tabs in vue.config.js

* single quotes and tabs

Converted vue.config.js to using single quotes
Converted example.spec.ts to tabs instead of spaces.

* Added semicolons and mocha import

* Update contributors

* Make vue 3 template linux/mac only

* Add tags to build

* Add v2 artefacts

* Update Vuetify in package.json (#537)

* Update contributors

* Replaced the old v-content tag with the new v-main (#536)

* v1.8.1-pre4

* v1.8.1-pre5

* Update issue templates (#541)

* Updated Contributors

* Remove zero copy string conversion

* v1.8.1-pre6

* Initial support for firebug (#543)

* Initial support for firebug

* Remove windows message

* v1.8.1-pre7

* Update contributors

* Allow use of custom HTML

* Allow use of custom HTML (#545)

* v1.8.1

Co-authored-by: Arthur Wiebe <arthur@artooro.com>
Co-authored-by: Ilgıt Yıldırım <ilgit.yildirim@triplebits.com>
Co-authored-by: Travis McLane <tmclane@gmail.com>
Co-authored-by: Altynbek <go.gelleson@gmail.com>
Co-authored-by: Kyle Muchmore <kyle.muchmore@kickview.com>
Co-authored-by: Balakrishna Prasad Ganne <balkripra.1996@gmail.com>
2020-10-29 20:18:52 +11:00

136 lines
3.1 KiB
Go

package renderer
import (
"time"
"github.com/gorilla/websocket"
"github.com/leaanthony/mewn"
"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()
wailsRuntime := mewn.String("../../runtime/assets/wails.js")
s.evalJS(wailsRuntime, 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...")
}