Client don't receive Data sent by server in "onopen"

Closed
kawtar_kaoutar Posts 54 Registration date Thursday July 3, 2014 Status Member Last seen June 28, 2015 - Jun 23, 2015 at 06:50 PM
abdelhamid84 Posts 1 Registration date Friday June 26, 2015 Status Member Last seen June 26, 2015 - Jun 26, 2015 at 03:28 PM
Hi everybody
I'm using websockets for developping a chat application with jee, I'm using java in backend and javascript in frontend. and when opening the websocket and want to get the id session from the server and use it in my client page.

Here is the code i was tried:

Server side:


@ServerEndpoint("/echo")
public class LiveStream {

@OnOpen
public void onOpen(Session session) {
try {
session.getBasicRemote().sendText(session.getId());
} catch (IOException ex) {
}

session.setMaxBinaryMessageBufferSize(1024 * 512);

}

@OnMessage
public void processVideo(byte[] imageData, Session session) {
try {
ByteBuffer buf = ByteBuffer.wrap(imageData);
Set set;
set = session.getOpenSessions();
System.out.println(set.size() + " this is the number of opened sessions");
Iterator it = set.iterator();
while (it.hasNext()) {
Session s = (Session) it.next();
if(s!=session){
s.getBasicRemote().sendBinary(buf);
}
}

} catch (IOException ex) {
} catch (Throwable ioe) {
System.out.println("Error sending message " + ioe.getMessage());
}
}

@OnClose
public void whenClosing(Session session) {
System.out.println("Goodbye !");
sessions.remove(session);
}

}


Client side:


<script>

window.onload = function () {
ws = new WebSocket("ws://localhost:8080/application/echo");
ws.onopen = function (event) {
if (event.data === undefined) return;
id = event.data;
document.getElementById('texxt').value += id;
};

ws.onmessage = function (msg) {
url = window.URL.createObjectURL(msg.data);
};

}


The problem is that the id session is not received from the server and in client side, event.data===undefined. and I have to access to id_session of each user to use it later.

I need your help please ans thank you
Related:

1 response

abdelhamid84 Posts 1 Registration date Friday June 26, 2015 Status Member Last seen June 26, 2015
Jun 26, 2015 at 03:28 PM
Welcom to kenitra
0