英文:
Golang Websocket detecting file message and writing it if more than 1 ws frame byte
问题
我需要将通过Websockets发送的每个新图片保存在一个文件中。以下是我的文件代码:
package main
import (
"fmt"
"golang.org/x/net/websocket"
"log"
"net/http"
"os"
"time"
)
var (
dirPath string
test byte
lenFile int
)
func check(e error) {
if e != nil {
panic(e)
}
}
func ChatServer(ws *websocket.Conn) {
defer ws.Close()
var test []byte
var payload []byte
for {
err := websocket.Message.Receive(ws, &payload)
if err != nil {
log.Println(err)
}
fmt.Println("Payload: ", len(payload))
test = append(test, payload...)
fmt.Println("Test: ", len(test))
fo, err := os.Create(fmt.Sprintf("./%d.png", time.Now().UnixNano()))
check(err)
_, err = fo.Write(test)
check(err)
fo.Close()
}
log.Print("DONE")
}
func main() {
if len(os.Args) < 2 {
log.Fatal("Usage: chatExample <dir>")
}
dirPath = os.Args[1]
fmt.Println("Starting... ")
http.Handle("/", http.FileServer(http.Dir(dirPath)))
http.Handle("/ws", websocket.Handler(ChatServer))
err := http.ListenAndServe(":3000", nil)
if err != nil {
log.Fatal("ListenAndServe ", err)
}
}
以下是按顺序下载两个文件的结果:
[![每个ws帧的字节的控制台日志][1]][1]
[1]: http://i.stack.imgur.com/QaTzk.png
[![我发送的两个图像][2]][2]
[2]: http://i.stack.imgur.com/9Qzz4.png
[![文件系统目录中的结果][3]][3]
[3]: http://i.stack.imgur.com/LHrbL.png
所以,如果我发送两个文件,我只需要保存两个文件(图像),而不是三个。此外,我需要保存两个不同的文件,我必须以某种方式从Websocket连接中检测到新文件。
英文:
I need to save each new picture in one file, which is sent by several frames through websockets. Here is code of my file:
package main
import (
"fmt"
"golang.org/x/net/websocket"
"log"
"net/http"
"os"
"time"
)
var (
dirPath string
test byte
lenFile int
)
func check(e error) {
if e != nil {
panic(e)
}
}
func ChatServer(ws *websocket.Conn) {
defer ws.Close()
var test []byte
var payload []byte
for {
err := websocket.Message.Receive(ws, &payload)
if err != nil {
log.Println(err)
}
fmt.Println("Payload: ", len(payload))
test = append(test, payload...)
fmt.Println("Test: ", len(test))
fo, err := os.Create(fmt.Sprintf("./%d.png", time.Now().UnixNano()))
check(err)
_, err = fo.Write(test)
check(err)
fo.Close()
}
log.Print("DONE")
}
func main() {
if len(os.Args) < 2 {
log.Fatal("Usage: chatExample <dir>")
}
dirPath = os.Args[1]
fmt.Println("Starting... ")
http.Handle("/", http.FileServer(http.Dir(dirPath)))
http.Handle("/ws", websocket.Handler(ChatServer))
err := http.ListenAndServe(":3000", nil)
if err != nil {
log.Fatal("ListenAndServe ", err)
}
}
And here is my results of downloading two files in order:
[![Console logging of bytes every ws frame][1]][1]
[1]: http://i.stack.imgur.com/QaTzk.png
[![Two images that I sent][2]][2]
[2]: http://i.stack.imgur.com/9Qzz4.png
[![Result in filesystem catalog][3]][3]
[3]: http://i.stack.imgur.com/LHrbL.png
So I need save only two files (images) if I send two files, not three. Also I need to save two different files, I have to somehow detect new file from websocket connect.
答案1
得分: 1
发送方将消息分成两个帧。由于x/net/websocket API暴露的是帧而不是消息,应用程序会写入三个文件。
使用x/net/websocket包无法检测消息边界。请改用gorilla/websocket包。
英文:
The sender fragments one of the messages into two frames. Because the x/net/websocket API exposes frames instead of messages, the application writes three files.
It's not possible to detect message boundaries using the x/net/websocket package. Use the gorilla/websocket package instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论