niv / websocket.nim

websockets for nim
http://niv.github.io/websocket.nim/docs/0.1.1/websocket.html
Other
101 stars 25 forks source link

ws.readData() unable to handle a null result #71

Open jfilby opened 1 year ago

jfilby commented 1 year ago

When I receive data from the websocket that has a null result, the program crashes.

In the attached test case, if you comment out the line that sends the subscribe request the program doesn't crash. This is because the message that is sent back includes a null result. However the websocket library should be able to handle this.

Here's the output from a Chrome websocket extension that does the same thing. The result from the subscribe request is null, but the websocket loop continues to function as expected.

{"e":"kline","E":1660633181407,"s":"BNBUSDT","k":{"t":1660633140000,"T":1660633199999,"s":"BNBUSDT","i":"1m","f":578704188,"L":578704249,"o":"317.00000000","c":"316.90000000","h":"317.10000000","l":"316.80000000","v":"111.62500000","n":62,"x":false,"q":"35378.63340000","V":"12.78200000","Q":"4052.67600000","B":"0"}}
{"e":"kline","E":1660633188428,"s":"BNBUSDT","k":{"t":1660633140000,"T":1660633199999,"s":"BNBUSDT","i":"1m","f":578704188,"L":578704250,"o":"317.00000000","c":"316.90000000","h":"317.10000000","l":"316.80000000","v":"111.65900000","n":63,"x":false,"q":"35389.40800000","V":"12.81600000","Q":"4063.45060000","B":"0"}}
{  "method": "SUBSCRIBE",  "params": [    "adausdt@kline_1m"  ],  "id": 1 }
{"result":null,"id":1}
{"e":"kline","E":1660633189014,"s":"ADAUSDT","k":{"t":1660633140000,"T":1660633199999,"s":"ADAUSDT","i":"1m","f":403020801,"L":403020878,"o":"0.55370000","c":"0.55320000","h":"0.55370000","l":"0.55310000","v":"39277.50000000","n":78,"x":false,"q":"21734.35789000","V":"14835.80000000","Q":"8208.74871000","B":"0"}}
{"e":"kline","E":1660633190748,"s":"BNBUSDT","k":{"t":1660633140000,"T":1660633199999,"s":"BNBUSDT","i":"1m","f":578704188,"L":578704251,"o":"317.00000000","c":"316.80000000","h":"317.10000000","l":"316.80000000","v":"111.75900000","n":64,"x":false,"q":"35421.08800000","V":"12.81600000","Q":"4063.45060000","B":"0"}}

test_ws.zip

AndrewGPU commented 1 year ago

When I receive data from the websocket that has a null result, the program crashes.

In the attached test case, if you comment out the line that sends the subscribe request the program doesn't crash. This is because the message that is sent back includes a null result. However the websocket library should be able to handle this.

Here's the output from a Chrome websocket extension that does the same thing. The result from the subscribe request is null, but the websocket loop continues to function as expected.

{"e":"kline","E":1660633181407,"s":"BNBUSDT","k":{"t":1660633140000,"T":1660633199999,"s":"BNBUSDT","i":"1m","f":578704188,"L":578704249,"o":"317.00000000","c":"316.90000000","h":"317.10000000","l":"316.80000000","v":"111.62500000","n":62,"x":false,"q":"35378.63340000","V":"12.78200000","Q":"4052.67600000","B":"0"}}
{"e":"kline","E":1660633188428,"s":"BNBUSDT","k":{"t":1660633140000,"T":1660633199999,"s":"BNBUSDT","i":"1m","f":578704188,"L":578704250,"o":"317.00000000","c":"316.90000000","h":"317.10000000","l":"316.80000000","v":"111.65900000","n":63,"x":false,"q":"35389.40800000","V":"12.81600000","Q":"4063.45060000","B":"0"}}
{  "method": "SUBSCRIBE",  "params": [    "adausdt@kline_1m"  ],  "id": 1 }
{"result":null,"id":1}
{"e":"kline","E":1660633189014,"s":"ADAUSDT","k":{"t":1660633140000,"T":1660633199999,"s":"ADAUSDT","i":"1m","f":403020801,"L":403020878,"o":"0.55370000","c":"0.55320000","h":"0.55370000","l":"0.55310000","v":"39277.50000000","n":78,"x":false,"q":"21734.35789000","V":"14835.80000000","Q":"8208.74871000","B":"0"}}
{"e":"kline","E":1660633190748,"s":"BNBUSDT","k":{"t":1660633140000,"T":1660633199999,"s":"BNBUSDT","i":"1m","f":578704188,"L":578704251,"o":"317.00000000","c":"316.80000000","h":"317.10000000","l":"316.80000000","v":"111.75900000","n":64,"x":false,"q":"35421.08800000","V":"12.81600000","Q":"4063.45060000","B":"0"}}

test_ws.zip

I don't understand your question, but when I try to run your code, there is no error? This project seems to be unmaintained

jfilby commented 1 year ago

I'll retest. Nim and this package have been updated since. This package just a few minutes ago as I write this.

AndrewGPU commented 1 year ago

哈哈,我今天推送了一下,希望作者合一下代码,结果作者就合了,并且改了一下代码,所以就更新了,本来我以为没有维护了