Data sent from server in "onopen" not received from client
Fermé
kawtar_kaoutar
Messages postés
54
Date d'inscription
Statut
Membre
Dernière intervention
-
Utilisateur anonyme -
Utilisateur anonyme -
Hi everyone
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:
Client side:
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
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
A voir également:
- Data sent from server in "onopen" not received from client
- App data - Guide
- Win setup from usb - Télécharger - Utilitaires
- Cybera client - Télécharger - Divers Réseau & Wi-Fi
- Apply update from adb traduction ✓ - Forum Téléphones & tablettes Android
- Cvbs in 1 - Forum Autoradio