如何在Golang中使用socket.io播放图像?

如何在Golang中使用socket.io播放图像?,go,socket.io,Go,Socket.io,我想使用socket.io在GolangServer端广播图像。我已经知道如何在Node.js中实现它,但我不知道如何使用Golang。代码如下: Node.jsf: var io = require('socket.io')(http); io.on('connection', function(socket){ fs.readFile(imagepath, (err, buf)=>{ socket.broadcast.emit('image', { i

我想使用socket.io在GolangServer端广播图像。我已经知道如何在Node.js中实现它,但我不知道如何使用Golang。代码如下: Node.jsf:

var io = require('socket.io')(http);
   io.on('connection', function(socket){
       fs.readFile(imagepath, (err, buf)=>{
       socket.broadcast.emit('image', { image: true, buffer: 
       buf.toString('base64') });
   }
戈诺工作:

type data struct {
    image  bool
    buffer string
}
server, _ := socketio.NewServer(nil)    
server.On("connection", func(socket socketio.Socket) {
                f, _ := os.Open(imagepath)
                reader := bufio.NewReader(f)
                content, _ := ioutil.ReadAll(reader)
                encoded := base64.StdEncoding.EncodeToString(content)
                socket.Emit("image", data{true, encoded})
        })
客户端使用Node.js与服务器端协作:

var img = document.getElementById('img')
var socket = io();
socket.on('image', function(info) {
if(info.image)
{ img.src = 'data:image/jpeg;base64,' + info.buffer;}
});

我自己找到了答案。 GO中数据结构的声明应为:

type data struct {
Image  bool   `json:"image"`
Buffer string `json:"buffer"`}
那就行了