简体   繁体   English

如何将信息包发送到WebSocket?

[英]How do I send a packet of information to a WebSocket?

So I'm trying to send a packet of information from a server to a socket and vice versa. 因此,我正在尝试将信息包从服务器发送到套接字,反之亦然。 I only see a way to send strings. 我只看到一种发送字符串的方法。 what I NEED to do is send a variable with a specific header that defines what type of data that variable is. 我需要做的是发送带有特定标头的变量,该标头定义该变量是什么类型的数据。 The data could be anything between a chat_Message to a player_move event. 数据可以是chat_Message到player_move事件之间的任何数据。

I would also like to send a list of information at once. 我也想一次发送信息列表。 This list would include 5 variables. 此列表将包含5个变量。 4 strings and an int. 4个字符串和一个int。 and I need to give that a header as well. 我还需要给它一个标题

The server is in C#.net SuperWebSockets, and the client is in javascript on a web server. 服务器位于C#.net SuperWebSockets中,客户端位于Web服务器上的javascript中。

Examples of code would also be nice, thanks in advnaced! 代码示例也很好,谢谢您!

Assuming you are going down the route of creating a JSON string to be sent and processed on a JavaScript server, guessing NodeJS? 假设您正沿着创建要在JavaScript服务器上发送和处理的JSON字符串的路线走下去,猜猜NodeJS吗? You should look at using a library like JSON.Net on the .Net side. 您应该在.Net端使用JSON.Net之类的库。

http://james.newtonking.com/json . http://james.newtonking.com/json

The above will provide examples of how you can convert a C# object or set of variables to a string which can be sent to a javascript server. 上面将提供有关如何将C#对象或变量集转换为可发送到javascript服务器的字符串的示例。

Once you are on the JavaScript side refer to the documentation for the server you are using to convert the string into a javascript object again. 一旦在JavaScript方面,请参考用于将字符串再次转换为javascript对象的服务器的文档。

Starter would be looking up the 'JSON.parse' function. 入门者将查找“ JSON.parse”函数。

Json is not a file format per say, you can store files in Json but that is incidental. Json不是说的文件格式,您可以将文件存储在Json中,但这是偶然的。 Json is a serialisation technique to convert an object and its associated objects and fields into a format appropriate for transmission or storage. Json是一种序列化技术,用于将对象及其关联的对象和字段转换为适合传输或存储的格式。 This seems to be exactly what you are looking to do. 这似乎正是您要执行的操作。

The other route is to go down is to use POST variables and/or URL encoding which is a more complicated approach to what you are trying to do. 另一种选择是使用POST变量和/或URL编码,这是您尝试执行的操作更为复杂的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM