Play 애플리케이션에 websocket 연결 설정이 있습니다. 이 함수는 아래에 있으며이 함수에서 일부 데이터를 스트리밍하려고합니다.Play Framework 2.1 웹 소켓 스트리밍 데이터가 작동하지 않습니다.
websocket 연결을 설정하고 갈 준비가되면 while
루프를 생성하여 임의 번호를 생성하여 프런트 엔드에 대한 websocket의 아웃 스트림으로 보냅니다.
이 내가이 테스트 한대로 내가 관찰 한 일이 있습니다
- 나는
while
루프 코드를 주석과 첫 번째 세out.write(...)
이있는 경우 예상대로, 그들은 모두 프런트 엔드에 도착. - 루프 코드
while
의 주석 처리를 제거하면 프런트 엔드에서 메시지를 수신하지 않습니다.while
루프가 주석 처리되면 처음 세 개의out.write(...)
도 사라집니다.
이것은 다소 당황 스럽습니다. 처음 세 개의 out.write(...)
메시지가 끊어지면 while
루프가 주석 처리되지 않은 경우 해당 메시지와 while
루프 out.write(...)
이 전달되지 않는 이유는 무엇입니까?
루프가 인쇄 문을 통해 확인되면 실행되지만 프런트 엔드에는 메시지가 표시되지 않습니다. 우리는 websocket의 루프에서 'out.write (...)'를 호출 할 수 있습니까?
public static WebSocket<String> realTimeChartConnection() {
return new WebSocket<String>() {
// called when the websocket is established
public void onReady(WebSocket.In<String> in, WebSocket.Out<String> out) {
// register a callback for processing instream events
in.onMessage(new Callback<String>() {
public void invoke(String event) {
System.out.println(event);
}
});
System.out.println("Websocket Connection ready ...");
out.write("50");
out.write("51");
out.write("60");
int prev = 50;
while (true) {
int y = (int) (prev + Math.random() * 10 - 5);
if (y < 0)
y = 0;
if (y > 100)
y = 100;
out.write(""+y);
try {
Thread.currentThread();
Thread.sleep(30);
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
}
}
};
}