this을 Haskell로 변환하려고 시도했습니다. 궁극적으로 나는 도관을 사용하고 싶지만, 지금은 게으른 ByteString이 할 것입니다.지연 줄을 사용한 스트리밍
그래서 나는 wrote this : 나는 그것을 시작하고 localhost:3000
을 열 때
{-# LANGUAGE OverloadedStrings, RecordWildCards #-}
module Main where
--import Data.Conduit.Binary
import Network.Wai
import Network.Wai.Handler.Warp
import Network.HTTP.Types
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Char8 as B
import Control.Monad.Trans
import System.IO
import Control.Exception
import Data.List.Split (splitOn)
video = "videos/big_buck_bunny.mp4"
main = bracket (openFile video ReadMode) (hClose) $ \h ->
run 3000 $ \Request{..} -> do
liftIO $ putStrLn "Connection"
total <- liftIO $ hFileSize h
case lookup "Range" requestHeaders of
Nothing -> do
v <- liftIO $ L.hGetContents h
return $ responseLBS ok200 [("Content-Type", "video/mp4"), ("Connection","keep-alive"), ("Content-Length", B.pack $ show total)] v
Just r -> do
let (starts:ends:_) = splitOn "-" $ drop 6 $ B.unpack r
start = read starts
end = if not (null ends) then read ends else total - 1
liftIO $ putStrLn $ "Range request " ++ show start ++ " " ++ show end
liftIO $ hSeek h AbsoluteSeek start
v <- liftIO $ L.hGetContents h
return $ responseLBS partialContent206 [("Content-Type", "video/mp4")
, ("Accept-Ranges", "bytes")
, ("Content-Length", B.pack . show $ (end - start) + 1)
, ("Content-Range", B.pack $ concat ["bytes ", show start, "-", show end, "/", show total])
] v
그러나, 나는 이러한 오류를 얻을 : 이러한 오류가 발생하는 이유
Connection
send: resource vanished (Connection reset by peer)
Connection
Range request 0 159240553
send: resource vanished (Connection reset by peer)
Connection
Range request 158852274 159240553
Connection
videos/big_buck_bunny.mp4: hFileSize: illegal operation (handle is closed)
Connection
videos/big_buck_bunny.mp4: hFileSize: illegal operation (handle is closed)
난 정말 이해가 안가, 대부분의 이유는 모두 resource vanished
입니다. 그리고 핸들이 제가 처리하지 않고 닫히는 이유도 hClose
입니다.
내가 잘못 생각한 사람이 있습니까?
닫지 않고 대체 할 수있는 것은 무엇입니까? – Lanbo
@ LambdaDusk : 없습니다. 핸들은 항상 파일의 단일 위치를 가리 킵니다. 모든 사용자에 대해 현저하게 하나의 핸들을 사용할 수 없습니다. 전체 파일을 메모리로 읽거나 많은 핸들을 사용할 수 있습니다. – Tener
그것은 방향의 한 걸음이지만, 내가 원하는대로 실제로 작동하지 않기 때문에, 나는 도관을 알아 내야합니다. 어쨌든 고마워! – Lanbo