From e0513563f275069ca123d273ba0c0caa3815f722 Mon Sep 17 00:00:00 2001 From: piaoshihuang Date: Mon, 26 Apr 2021 08:51:46 +0000 Subject: [PATCH 1/2] add buffer support --- package-lock.json | 4 ++-- package.json | 4 ++-- src/trans-websocket.coffee | 4 +++- src/transport.coffee | 27 +++++++++++++++++++++------ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 992e164e..7a006bab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "sockjs", - "version": "0.3.21", + "name": "sockjs-adv", + "version": "0.3.24", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index f3315fc2..69238d6e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "sockjs", + "name": "sockjs-adv", "description": "SockJS-node is a server counterpart of SockJS-client a JavaScript library that provides a WebSocket-like object in the browser. SockJS gives you a coherent, cross-browser, Javascript API which creates a low latency, full duplex, cross-domain communication channel between the browser and the web server.", - "version": "0.3.21", + "version": "0.3.27", "author": "Marek Majkowski", "bugs": { "url": "https://github.com/sockjs/sockjs-node/issues" diff --git a/src/trans-websocket.coffee b/src/trans-websocket.coffee index 57716faf..bccca80f 100644 --- a/src/trans-websocket.coffee +++ b/src/trans-websocket.coffee @@ -68,7 +68,9 @@ class WebSocketReceiver extends transport.GenericReceiver try message = JSON.parse(payload) catch x - return @didClose(3000, 'Broken framing.') + # return raw message when not json string + message = payload + # return @didClose(3000, 'Broken framing.') if payload[0] is '[' for msg in message @session.didMessage(msg) diff --git a/src/transport.coffee b/src/transport.coffee index 3d370ca8..7ce6fac5 100644 --- a/src/transport.coffee +++ b/src/transport.coffee @@ -28,8 +28,12 @@ class SockJSConnection extends stream.Stream toString: -> return '' - write: (string) -> - return @_session.send('' + string) + write: (data) -> + # 二进制数据 + if(data instanceof Buffer) + return @_session.send(data) + else + return @_session.send('' + data) end: (string) -> if string @@ -198,7 +202,10 @@ class Session send: (payload) -> if @readyState isnt Transport.OPEN return false - @send_buffer.push('' + payload) + if payload instanceof Buffer + @send_buffer.push(payload) + else + @send_buffer.push('' + payload) if @recv @tryFlush() return true @@ -265,9 +272,17 @@ class GenericReceiver @session.unregister() doSendBulk: (messages) -> - q_msgs = for m in messages - utils.quote(m) - @doSendFrame('a' + '[' + q_msgs.join(',') + ']') + q_msgs = [] + for m in messages + if !(m instanceof Buffer) + q_msgs.push(utils.quote(m)) + else + if q_msgs.length > 0 + @doSendFrame('a' + '[' + q_msgs.join(',') + ']') + q_msgs = [] + @doSendFrame(m) + if q_msgs.length > 0 + @doSendFrame('a' + '[' + q_msgs.join(',') + ']') heartbeat: -> @doSendFrame('h') From 9ee8d4d10947f1003c968b028a7e9c57d3df151a Mon Sep 17 00:00:00 2001 From: piaoshihuang Date: Mon, 26 Apr 2021 09:16:39 +0000 Subject: [PATCH 2/2] fix package name --- package.json | 4 ++-- src/transport.coffee | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 69238d6e..f3315fc2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "sockjs-adv", + "name": "sockjs", "description": "SockJS-node is a server counterpart of SockJS-client a JavaScript library that provides a WebSocket-like object in the browser. SockJS gives you a coherent, cross-browser, Javascript API which creates a low latency, full duplex, cross-domain communication channel between the browser and the web server.", - "version": "0.3.27", + "version": "0.3.21", "author": "Marek Majkowski", "bugs": { "url": "https://github.com/sockjs/sockjs-node/issues" diff --git a/src/transport.coffee b/src/transport.coffee index 7ce6fac5..8f2b8530 100644 --- a/src/transport.coffee +++ b/src/transport.coffee @@ -29,7 +29,7 @@ class SockJSConnection extends stream.Stream return '' write: (data) -> - # 二进制数据 + # binary buffer if(data instanceof Buffer) return @_session.send(data) else