You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WebSocket服务器读取数据出现乱码问题求助

Hey there! Let's dig into this WebSocket encoding issue you're facing—handshake works but messages turn to gibberish? Super frustrating, but let's break down the most likely culprits and fixes:

Common Causes & Troubleshooting Steps

1. Mismatched Text Encoding Between Client and Server

This is the #1 reason for WebSocket message garbling. WebSocket defaults to UTF-8, but if one end uses a different encoding (like GBK, ISO-8859-1) while the other expects UTF-8, you’ll get gibberish.

  • Client check: If you’re using raw browser WebSocket, make sure you’re sending plain strings (e.g., ws.send("9")) instead of manually converting to an ArrayBuffer with a non-UTF-8 encoding. If using a framework, verify its default message encoding is set to UTF-8.
  • Server check: When decoding incoming messages, confirm you’re using UTF-8. For example, in Node.js, use buffer.toString('utf8') instead of toString('ascii') or other encodings.

2. Mixing Up Binary vs. Text Message Types

WebSocket supports two message modes: text and binary. If you send a binary message but parse it as text (or vice versa), you’ll get unreadable output.

  • Client side: Double-check you’re not accidentally sending an ArrayBuffer/Blob when you mean to send text. For example, avoid ws.send(new ArrayBuffer(1)) if you just want to send the string "9".
  • Server side: Ensure your WebSocket library is handling message types correctly. Some frameworks auto-detect type, but misconfigurations can force binary messages to be parsed as text (or the other way around).

3. Partial Message/Fragmentation Issues

WebSocket messages can be split into fragments during transmission. If your code doesn’t properly assemble these fragments before decoding, you’ll get corrupted text.

  • Server check: If you’re using a low-level WebSocket implementation, make sure you’re listening for complete message events (most modern libraries like Node.js ws handle this automatically, but custom code might not).
  • Client check: In the browser’s onmessage event, confirm you’re processing the full event.data—don’t try to decode partial chunks manually.

4. Middleware/Proxy Data Modification

If your WebSocket connection goes through a proxy, load balancer, or middleware, these tools might accidentally modify message content (e.g., incorrect transcoding, adding extra characters).

  • Test connecting directly to your WebSocket server (bypassing any proxies). If the garbling stops, you’ll need to adjust the proxy’s configuration to leave WebSocket messages untouched (they should be treated as opaque binary streams).

Quick Diagnostic Test

To narrow it down fast:

  1. Have your client send the string "9" directly.
  2. On the server, log the raw byte data of the incoming message. The UTF-8 byte for "9" is 0x39.
    • If the server receives 0x39 but decodes to gibberish: The decoding step on the server is using the wrong encoding.
    • If the server receives something other than 0x39: The client is sending the message with the wrong encoding or as binary.

内容的提问来源于stack exchange,提问作者Luicy Willowfield

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:42:15