Golang Websocket 检测文件消息,并在超过1个 WebSocket 帧字节时进行写入。

huangapple go评论96阅读模式
英文:

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 (
&quot;fmt&quot;
&quot;golang.org/x/net/websocket&quot;
&quot;log&quot;
&quot;net/http&quot;
&quot;os&quot;
&quot;time&quot;
)
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, &amp;payload)
if err != nil {
log.Println(err)
}
fmt.Println(&quot;Payload: &quot;, len(payload))
test = append(test, payload...)
fmt.Println(&quot;Test: &quot;, len(test))
fo, err := os.Create(fmt.Sprintf(&quot;./%d.png&quot;, time.Now().UnixNano()))
check(err)
_, err = fo.Write(test)
check(err)
fo.Close()
}
log.Print(&quot;DONE&quot;)
}
func main() {
if len(os.Args) &lt; 2 {
log.Fatal(&quot;Usage: chatExample &lt;dir&gt;&quot;)
}
dirPath = os.Args[1]
fmt.Println(&quot;Starting... &quot;)
http.Handle(&quot;/&quot;, http.FileServer(http.Dir(dirPath)))
http.Handle(&quot;/ws&quot;, websocket.Handler(ChatServer))
err := http.ListenAndServe(&quot;:3000&quot;, nil)
if err != nil {
log.Fatal(&quot;ListenAndServe &quot;, 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.

huangapple
  • 本文由 发表于 2016年3月10日 07:14:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/35904565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定