mercredi 5 août 2015

How to stream Data in with java websockets


I read some tutorials or tried it so now I have some JS code that connects with my java websocket and when I click on a button it sends some test string to the websocket and reciev some other.

Now I wanna have some realtime stream so in the string that the JS code recieves is the current time in milliseconds how can I achiev that I get don't know every second get the new time ? Maybe it would be an better idea to use normal time then instead of milliseconds so I can change this.

My codes so far Javascript

var websocket;
$(document).ready(function(){
    websocket = new WebSocket("ws://localhost:8080/TimeStreamingTestartID-1.0-SNAPSHOT/websockets/simplestockmarket")

    websocket.onopen = function(){ 
    alert("SUCESS: REGISTERED!!");
    }

$("#start").click(function(){

    websocket.send("Test")
    websocket.onmessage = function(erg){
    console.log(erg);
    }

});

});

My java WebSocket code

@ServerEndpoint("/websockets/simplestockmarket")
public class SimpleStockMarketWSServerEndpoint {

@OnOpen
public void open(Session session) {


}



   @OnMessage
    public void message(Session session,String message) {

        try {


session.getBasicRemote().sendText(Long.toString(System.currentTimeMillis())+" <- time, message from client -> "+message);
        } catch (IOException e) {
            e.printStackTrace();
        }

}

@OnClose
public void close(Session session) {

}
}

I know it's not finished yet but I wanna achiev this streaming before I do the rest like closing the connection again. If I missed something important please tell me and I will add it.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire