Golang与类型的工作

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

Golang Working with types

问题

问题

我在使用Golang中的类型时遇到了一些困惑,我需要一些帮助。我正在尝试修改Gorilla的websocket聊天示例,并且我想要传入JSON并相应地处理它。例如,我想要设置一个用户,并向websocket发送一条消息。

在当前的实现中,它只是接收一个字符串,并通过广播将其发送回所有连接的客户端。

例如,我想要能够设置以下数据结构并以不同的方式处理它。

传入的用户将是:

{
  'type': 'user',
  'value': 'John Doe'
}

传入的消息将是:

{
  'type': 'message',
  'value': 'Hi There, how are you?'
}

请参考以下代码:
https://github.com/gorilla/websocket/blob/master/examples/chat/conn.go#L52

for {
	_, message, err := c.ws.ReadMessage()
	if err != nil {
		break
	}
	h.broadcast <- message
}

数据

所以上述代码适用于单个字符串,因为服务器所做的只是将任何客户端的传入消息传递给所有客户端。但是,我想要能够保存用户,并将活动用户列表与新消息一起发送回客户端。

为了做到这一点,在我看来,我将需要以不同的方式处理不同的传入消息。我尝试对传入的“message”进行解组,但是当我打印出类型时,它是这样的:

2015/11/24 20:03:10 []uint8

问题

我如何读取此输入流并将其解组为JSON?这是我在该部分添加的调试信息:

for {
	_, dataString, err := c.ws.ReadMessage()
	if err != nil {
		break
	}

	log.Println(reflect.TypeOf(dataString))

	var data MessageData
	err = json.Unmarshal(dataString, &data)
	log.Println(data.Type)
	log.Println(data.Value)

	if err != nil {
		log.Println(err)
	}

	h.broadcast <- dataString
}

返回结果

2015/11/24 20:03:10 []uint8
2015/11/24 20:03:10 
2015/11/24 20:03:10
英文:

The Problem

I need some help working with types in Golang, I get super confused. I am trying to alter the websocket chat example from Gorilla, and I want to pass in JSON and process it accordingly. I need to be able to set for example a user, and send a message to the websocket.

In the current implementation all it does is receive a string, and relays it back by broadcasting it to all connected clients.

For example I want to be able to set a data structure as follows and process it differently.

Incoming user would be:

{
  &#39;type&#39;: &quot;user&quot;,
  &#39;value&#39;: &quot;John Doe&quot;
}

Incomming message would be:

{
  &#39;type&#39;: &quot;message&quot;,
  &#39;value&#39;: &quot;Hi There, how are you?&quot;
}

Please refer to the following code:
https://github.com/gorilla/websocket/blob/master/examples/chat/conn.go#L52

for {
	_, message, err := c.ws.ReadMessage()
	if err != nil {
		break
	}
	h.broadcast &lt;- message
}

Data

So the above code works for single strings, because all the server does is pass the incoming message from any client to all. But I want to be able to save users, and send the list of active users back to the client along with the new message.

To do this, in my mind I will need to handle the different incoming messages differently. I tried to unmarchal the incomming message, but when I print out the type it's this:

2015/11/24 20:03:10 []uint8

THE QUESTION

How do I go about reading this input stream, and unmarshal it to JSON? This is the debugging I added to the section:

for {
	_, dataString, err := c.ws.ReadMessage()
	if err != nil {
		break
	}

	log.Println(reflect.TypeOf(dataString))

	var data MessageData
	err = json.Unmarshal(dataString, &amp;data)
	log.Println(data.Type)
	log.Println(data.Value)

	if err != nil {
		log.Println(err)
	}

	h.broadcast &lt;- dataString
}

Which Returns

2015/11/24 20:03:10 []uint8
2015/11/24 20:03:10 
2015/11/24 20:03:10 

答案1

得分: 3

抱歉,我在这方面有点愚蠢。message已经是一个字节类型的接口。我在将数据发送到后端之前,没有正确地对HTML / JavaScript前端进行编码。

以下是在前端对其进行字符串化的方法:

var data = {};
data.type = "user";
data.value = user.val();
conn.send(JSON.stringify(data));
JSON.stringify(data);

然后,我可以解析JSON数据:

2015/11/24 20:36:04 {user fhgfhg}
2015/11/24 20:36:04 user
2015/11/24 20:36:04 fhgfhg
英文:

Sorry, I was being a bit stupid with this. The message is already a byte type interface. I didn't properly encode the data on the HTML / JavaScript frontend before sending it to the backend.

What worked was stringifying it in the frontend:

var data = {};
data.type = &quot;user&quot;;
data.value = user.val();
conn.send(JSON.stringify(data));
JSON.stringify(data)

Then I was able to unmarshall the JSON:

2015/11/24 20:36:04 {user fhgfhg}
2015/11/24 20:36:04 user
2015/11/24 20:36:04 fhgfhg

huangapple
  • 本文由 发表于 2015年11月25日 02:22:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/33901055.html
匿名

发表评论

匿名网友

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

确定