我实际上使用Spring Famework 4.3.2为STSP实现了一个用于WebSocket的Java SockJS客户端。 我的Javascript客户端创建了Java客户端不创建的nativeHeaders。
Java客户端:
var from = "192:168:99:3::DEMO";
var targetNickname = "";
stompClient.connect({tokenGroup:from, targetNickname: targetNickname}, function(frame) {
...
}
JavaScript的:
request.getHeaders(): {Origin=[localhost:8080], Sec-WebSocket-Key=[tVFgflo2od6obTCKW/Y1Ng==],
Connection=[Upgrade], Sec-WebSocket-Version=[13], Host=[localhost:8080],
Upgrade=[websocket],
**targetNickname=[NULL_BORRAR], tokenGroup=[192:168:99:3::DEMO]**}
在服务器端,使用ChannelInterceptorAdapter的实现来查看输出。 JavaScript客户端包括tokenGroup和targetNickname到nativeHeaders。
Java客户端websocket升级握手:
message: GenericMessage [payload=byte[0], headers={simpMessageType=CONNECT, stompCommand=CONNECT, **nativeHeaders={heart-beat=[10000,10000], accept-version=[1.1,1.2]}**, simpSessionAttributes={ip=/127.0.0.1:53640}, simpHeartbeat=[J@907d80, simpSessionId=2c150f9d47504b76a896ada101e8e1bc}]
Java客户端websocket CONNECT Stomp消息:
request.getHeaders(): {Origin=[http://localhost:3000], Sec-WebSocket-Key=[slsZkCofKgaFZyxsADbzlw==],
User-Agent=[Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36],
Connection=[Upgrade], Sec-WebSocket-Version=[13], Host=[localhost:8080],
Accept-Encoding=[gzip, deflate, sdch], DNT=[1], Pragma=[no-cache],
Upgrade=[websocket], Sec-WebSocket-Extensions=[permessage-deflate; client_max_window_bits], Cache-Control=[no-cache], Accept-Language=[en-US,en;q=0.8]}
JavaScript客户端websocket升级握手:
message: GenericMessage [payload=byte[0], headers={simpMessageType=CONNECT, stompCommand=CONNECT, **nativeHeaders={tokenGroup=[192:168:99:4::DEMO], targetNickname=[], accept-version=[1.1,1.0], heart-beat=[10000,10000]}**, simpSessionAttributes={ip=/127.0.0.1:55150}, simpHeartbeat=[J@915deb, simpSessionId=g3pcbfvj}]
JavaScript客户端Websocket CONNECT Stomp消息:
Controller:
function startfunction(){
$rootScope.$broadcast('sharingFn');
}
startfunction();
Directive:
$scope.$on('sharingFn',function(){
console.log('function called");
}
您可以看到Java客户端有 nativeHeaders = {heart-beat = [10000,10000],accept-version = [1.1,1.2]} ,Javacript有 nativeHeaders = {tokenGroup = [192:168:99:4 :: DEMO],targetNickname = [],accept-version = [1.1,1.0],heart-beat = [10000,10000]}
如何将“tokenGroup”包含到Java客户端中? Java和JavaScript客户端的工作方式是否正常?
答案 0 :(得分:0)
解决方案:
您需要使用包含StompHeaders的重载方法。
WebSocketHttpHeaders handshakeHeaders = new WebSocketHttpHeaders();
StompHeaders connectHeaders = new StompHeaders();
connectHeaders.add("tokenGroup", "192:168:99:3::DEMO");
stompClient.connect(stompUrlEndpoint.toString(), handshakeHeaders, connectHeaders, stompSessionHandlerImp, new Object[0]);
现在这会在服务器中生成下一个:
message: GenericMessage [payload=byte[0], headers={simpMessageType=CONNECT, stompCommand=CONNECT, nativeHeaders={tokenGroup=[192:168:99:3::DEMO], heart-beat=[10000,10000], accept-version=[1.1,1.2]}, simpSessionAttributes={ip=/127.0.0.1:57773}, simpHeartbeat=[J@1ac7a81, simpSessionId=a1648581b94d422e8b88258efad36ffb}]
nativeHeader现在在connect上包含tokenGrouo标头。