依赖注入不适用于websockets

时间:2017-06-30 05:41:52

标签: java spring websocket

这是我面临的一段代码问题。当post构造被调用时...源被设置..但是当我从服务器收到一条消息并且processBinaryMessage方法被调用时...源被证明为null。

我不明白这个问题......感谢任何帮助..

@ClientEndpoint
@Component
public class MyClientEndpoint {

    @Inject
    private Source Source;

    private boolean postConstructCalled = false;

    @PostConstruct
    public void init() {
        postConstructCalled = true;
        System.out.println("Post construct called ... ");
    }

    @OnOpen
    public void onOpen(Session session) {
        System.out.println("Connected to endpoint: " + session.getBasicRemote());
        SessionUtil.setSession(session);
        try {
            System.out.println("Checking the established connection........");
            session.getBasicRemote().sendText("Checking the connection...");

        } catch (IOException ex) {
            Logger.getLogger(MyClientEndpoint.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @OnMessage
    public void processMessage(String message) {
        System.out.println("Received string message in client: " + message);
    }

    @OnMessage
    public void processBinaryMessage(byte[] bytes) {

        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        byte[] type = new byte[3];
        byteBuffer.get(type);
        String msgType = new String(type);
        //System.out.println("Read type : " + msgType);

        if(msgType.compareTo("MAX") == 0) {
            int maxLimit =  byteBuffer.getInt();
            System.out.println("Read max blocks : " + maxLimit);
            ClientDataTransfer.setMaxTransferLimit(maxLimit);
        }
        else if(msgType.compareTo("NXT") == 0) {
            ClientDataTransfer.incMaxTransferLimit();
        }
        else if(msgType.compareTo("MAP") == 0) {
            int length =  byteBuffer.getInt();
            byte[] data = new byte[length];
            byteBuffer.get(data);
            source.getClientDataTransfer().addToDataRequestQueue(data);
        }
    }

    @OnError
    public void processError(Throwable t) {
        t.printStackTrace();
    }

    @OnClose
    public void onClose(Session session) {
        try {
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Closing connection to endpoint: " + session.getBasicRemote());
    }

}

谢谢, Sreeja

0 个答案:

没有答案