您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

livereload.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* ----------------------------------------------------------------------
  2. * ______ ___ __
  3. * / ____/___ / | ____ __ ___ __/ /_ ___ ________
  4. * / / __/ __ \/ /| | / __ \/ / / / | /| / / __ \/ _ \/ ___/ _ \
  5. * / /_/ / /_/ / ___ |/ / / / /_/ /| |/ |/ / / / / __/ / / __/
  6. * \____/\____/_/ |_/_/ /_/\__. / |__/|__/_/ /_/\___/_/ \___/
  7. * /____/
  8. *
  9. * (C) Copyright 2015 GoAnywhere (http://goanywhere.io).
  10. * ----------------------------------------------------------------------
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. * ----------------------------------------------------------------------*/
  23. package livereload
  24. import (
  25. "encoding/json"
  26. "net/http"
  27. "sync"
  28. "github.com/gorilla/websocket"
  29. )
  30. /* ----------------------------------------------------------------------
  31. * WebSocket Server
  32. * ----------------------------------------------------------------------*/
  33. var (
  34. once sync.Once
  35. broadcast chan []byte
  36. tunnels map[*tunnel]bool
  37. in chan *tunnel
  38. out chan *tunnel
  39. mutex sync.RWMutex
  40. upgrader = websocket.Upgrader{
  41. ReadBufferSize: 1024,
  42. WriteBufferSize: 1024,
  43. }
  44. URL = struct {
  45. WebSocket string
  46. JavaScript string
  47. }{
  48. WebSocket: "/livereload",
  49. JavaScript: "/livereload.js",
  50. }
  51. )
  52. // Alert sends a notice message to browser's livereload.js.
  53. func Alert(message string) {
  54. go func() {
  55. var bytes, _ = json.Marshal(&alert{
  56. Command: "alert",
  57. Message: message,
  58. })
  59. broadcast <- bytes
  60. }()
  61. }
  62. // Reload sends a reload message to browser's livereload.js.
  63. func Reload() {
  64. go func() {
  65. var bytes, _ = json.Marshal(&reload{
  66. Command: "reload",
  67. Path: URL.WebSocket,
  68. LiveCSS: true,
  69. })
  70. broadcast <- bytes
  71. }()
  72. }
  73. // run watches/dispatches all tunnel & tunnel messages.
  74. func run() {
  75. for {
  76. select {
  77. case tunnel := <-in:
  78. mutex.Lock()
  79. defer mutex.Unlock()
  80. tunnels[tunnel] = true
  81. case tunnel := <-out:
  82. mutex.Lock()
  83. defer mutex.Unlock()
  84. delete(tunnels, tunnel)
  85. close(tunnel.message)
  86. case m := <-broadcast:
  87. for tunnel := range tunnels {
  88. select {
  89. case tunnel.message <- m:
  90. default:
  91. mutex.Lock()
  92. defer mutex.Unlock()
  93. delete(tunnels, tunnel)
  94. close(tunnel.message)
  95. }
  96. }
  97. }
  98. }
  99. }
  100. // Serve serves as a livereload server for accepting I/O tunnel messages.
  101. func ServeWebSocket(w http.ResponseWriter, r *http.Request) {
  102. var socket, err = upgrader.Upgrade(w, r, nil)
  103. if err != nil {
  104. return
  105. }
  106. tunnel := new(tunnel)
  107. tunnel.socket = socket
  108. tunnel.message = make(chan []byte, 256)
  109. in <- tunnel
  110. defer func() { out <- tunnel }()
  111. tunnel.connect()
  112. }
  113. // ServeJavaScript serves livereload.js for browser.
  114. func ServeJavaScript(w http.ResponseWriter, r *http.Request) {
  115. w.Header().Set("Content-Type", "application/javascript")
  116. w.Write(javascript)
  117. }
  118. // Start activates livereload server for accepting tunnel messages.
  119. func Start() {
  120. once.Do(func() {
  121. broadcast = make(chan []byte)
  122. tunnels = make(map[*tunnel]bool)
  123. in = make(chan *tunnel)
  124. out = make(chan *tunnel)
  125. go run()
  126. })
  127. }