当前位置:  开发笔记 > 编程语言 > 正文

Suave in watch模式(开发期间)

如何解决《Suaveinwatch模式(开发期间)》经验,为你挑选了1个好方法。

我正在研究Suave 1.0 + Angular 2.0示例应用程序并且在监视模式下启动Suave服务器非常有趣,因此服务器监视文件在根文件夹和子文件夹中更改(js,css,html)并自动向refresh所有打开的浏览器发送命令任何文件更改时,我的应用程序的选项卡.

lite-server来自Angular 2 5min Quckstark可以做到这一点,它非常方便.

我认为大多数表款都可以在最新的Steffen Forkmann的帖子中找到,但是如何发送refresh到开放的浏览器标签并不是很干净.

请提供类似实现的完整代码Suave.



1> Sergey Tihon..:

Suave服务器的代码应该与此类似

#r "packages/Suave/lib/net40/suave.dll"
#r "packages/FAKE/tools/FakeLib.dll"

open Fake
open Suave
open Suave.Operators
open Suave.Sockets.Control
open Suave.WebSocket
open Suave.Utils
open Suave.Files
open Suave.RequestErrors
open Suave.Filters
open System
open System.Net


let port =
    let rec findPort port =
        let portIsTaken =
            System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()
            |> Seq.exists (fun x -> x.Port = int(port))
        if portIsTaken then findPort (port + 1us) else port
    findPort 8083us

let logger = Logging.Loggers.ConsoleWindowLogger Logging.LogLevel.Verbose

let refreshEvent = new Event<_>()

let handleWatcherEvents (events:FileChange seq) =
    for e in events do
        let fi = fileInfo e.FullPath
        traceImportant <| sprintf "%s was changed." fi.Name
    refreshEvent.Trigger()

let socketHandler (webSocket : WebSocket) =
  fun cx -> socket {
    while true do
      let! refreshed =
        Control.Async.AwaitEvent(refreshEvent.Publish)
        |> Suave.Sockets.SocketOp.ofAsync
      do! webSocket.send Text (ASCII.bytes "refreshed") true
  }

let cfg =
  { defaultConfig with
      homeFolder = Some (__SOURCE_DIRECTORY__)
      bindings =
        [ HttpBinding.mk HTTP IPAddress.Loopback port ]
      listenTimeout = TimeSpan.FromMilliseconds 3000. }

let app : WebPart =
  choose [
    Filters.log logger logFormat >=> never
    Filters.path "/websocket" >=> handShake socketHandler
    Filters.GET >=> Filters.path "/" >=> file "index.html"
    Writers.setHeader "Cache-Control" "no-cache, no-store, must-revalidate"
      >=> Writers.setHeader "Pragma" "no-cache"
      >=> Writers.setHeader "Expires" "0"
      >=> browseHome
    NOT_FOUND "Found no handlers."
  ]


let watcher =
    !! ("app/*.js")
      ++ ("*.html")
    |> WatchChanges handleWatcherEvents

try
    System.Diagnostics.Process.Start(sprintf "http://localhost:%d/index.html" port) |> ignore
    startWebServer cfg app
finally
    watcher.Dispose()

因此我们设置了处理器来处理js(由TypeScript生成)和html文件的变化并将refresh命令发送到客户端,但同时我们需要将以下代码添加到客户端处理的head部分index.htmlrefresh



您可以在这里找到完整的演示应用程序

推荐阅读
吻过彩虹的脸_378
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有