簡體   English   中英

Go 中接收 Javascript 對象的處理函數(使用 socket.io)

[英]Handling function in Go that receives a Javascript object (using socket.io)

我有來自客戶端的以下代碼(用 jQuery 編寫):

$("#send").on("click", function (e) {
    e.preventDefault();
    var content = $("#msg-content").val().trim();
    //emit a socket, sending an object to the server
    socket.emit("createMessage", {
        user: "Lam",
        content: content
    });
    $("#msg-content").val('');
});

和來自服務器的代碼

import "github.com/googollee/go-socket.io"

Server.On("connection", func(so socketio.Socket) {
        logger.Info("on connection")
        //what parameter and its type should I put inside the func?
        so.On("createMessage", func(type?) {

        })
})

我的問題是如何將 js 對象從客戶端傳遞到“createMessage”事件中?

我真的很感激任何幫助。 抱歉,我無法縮進代碼。

從我在關於Server Side的例子中看到的函數需要一個字符串。 此外,您所指的對象是一個 JSON 對象 - 所以它是字符串。 我想你想解組(解碼)那個 JSON 字符串,以便用它做一些事情。 我敢冒險,您需要的是以下內容:

import "github.com/googollee/go-socket.io"

Server.On("connection", func(so socketio.Socket) {
        logger.Info("on connection")
        //what parameter and its type should I put inside the func?
        so.On("createMessage", handlePayload)
})

func handlePayload(msg string) {
    // do something with the JSON string in the msg
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM