通过websockets传递数据(有效载荷除外)

时间:2015-01-31 10:30:02

标签: java spring session websocket spring-websocket

一旦建立了websocket会话,是否可以在会话中传递除使用session.getAsyncRemote()。sendText(**)之外的数据?

我正在使用  javax.websocket。*作为ClientEndpoint  和spring-websocket用于服务器。

我尝试过使用:

session.getRequestParameterMap().putAll(bMap); //This gives me exception 
session.getPathParameters().putAll(aMap); //This gives me exception
session.getUserProperties().put("dataE", "dataF");; //This goes through fine 

我无法在服务器端看到数据:

Map<String, Object> aMap = stStandard.getNativeSession().getUserProperties();

此地图在服务器端显示为空白。如果需要更多信息,请告诉我。这是我的代码:

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.websocket.ClientEndpoint;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;

@ClientEndpoint
public class TestSpring {
            @OnMessage
            public void onMessage(String message, Session session) throws IOException,
                    InterruptedException {
                Map aMap = new HashMap();
                aMap.put("dataA", "dataB");
                try {
                    session.getPathParameters().putAll(aMap);
                } catch (Exception e) {
                    e.printStackTrace();
                }           
                Map bMap = new HashMap();

                List<String> bList = new ArrayList<String>();

                bList.add("dataC");
                try {
                    bMap.put("dataD", bList);
                    session.getRequestParameterMap().putAll(bMap);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    session.getUserProperties().put("dataE", "dataF");
                } catch (Exception e) {
                    e.printStackTrace();
                }            
                session.getAsyncRemote().sendText("Data to be sent");
            }

            @OnOpen
            public void onOpen() {
                System.out.println("Client connected");
            }            
            @OnClose
            public void onClose() {
                System.out.println("Connection closed");
            }

        }




import java.util.List;
import java.util.Map;

import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketExtension;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.adapter.standard.StandardWebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;

import com.sap.websocket.inMemoryWebSockets.KeepInMemoryWebSockets;

public class WebsocketEndPoint extends AbstractWebSocketHandler {   

    @Override
    protected void handleTextMessage(WebSocketSession session,
            TextMessage message) throws Exception {
        session.getHandshakeHeaders();
        StandardWebSocketSession stStandard = (StandardWebSocketSession) session;
        try {
            Map<String, Object> aMap = stStandard.getNativeSession()
                    .getUserProperties();
        } catch (Exception e) {
            e.printStackTrace();
        }   

        TextMessage binaryMessage = new TextMessage(new String(
                "Hello Client. This is message sent from server"));
        session.sendMessage(binaryMessage);
    }

    @Override
    protected void handleBinaryMessage(WebSocketSession session,
            BinaryMessage message) throws Exception {           
    }    
}

0 个答案:

没有答案