Java Spring Audio chat

2.7k views Asked by At

How can I develop web application that allow audio chat to users, using Java and Spring? I have used WebSocket within Java Web 7 ([https://tyrus.java.net/][Project Tyrus]), and it works great with text massages.


But for audio chat I can't figure it out. I tried to use WebRTC to detect microphone, here is the client side code:

     var ws2 = "ws://localhost:8080/WindTest/voiceChat/asd/username";
 var websocket = new WebSocket(ws2);

 var session = {
 audio: true,
 video: false
  };
  function initializeRecorder(stream) {
  var audioContext = window.AudioContext;
  var context = new audioContext();
  var audioInput = context.createMediaStreamSource(stream);
  var bufferSize = 2048;
 // create a javascript node
 //var recorder = context.createJavaScriptNode(bufferSize, 1, 1);
 var recorder = context.createScriptProcessor(bufferSize, 1, 1);
 // specify the processing function
 recorder.onaudioprocess = sendAudio;
 // connect stream to our recorder
 audioInput.connect(recorder);
 // connect our recorder to the previous destination
 recorder.connect(context.destination);
 }

 function onError(error){
 console.log(error);
 }
 navigator.getUserMedia(session, initializeRecorder, onError);

 function sendAudio(e) {
 var left = e.inputBuffer.getChannelData(0);
 //Send audio over websocket
 websocket.send(msg);
 }

Server side:

@ServerEndpoint(value = "/voiceChat/{room}/{username}")
public class VoiceChatEndPoint {

    @OnMessage
    public void broadcastMsg(Object object, Session session) {
        boolean roomExist = false;
        for (Session peer : session.getOpenSessions()) {
            try {
                peer.getBasicRemote().sendObject(object);
                //                    roomExist = true;
            } catch (IOException ex) {
                Logger.getLogger(VoiceChatEndPoint.class.getName()).log(Level.SEVERE, null, ex);
            } catch (EncodeException ex) {
                Logger.getLogger(VoiceChatEndPoint.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

I have got an exception, I think it's about decoding and encoding, but I'm not sure how to handle it:

Info:   Unhandled exception in endpoint com.wind.ptt.VoiceChatEndPoint.
java.lang.IllegalStateException: Text message handler not found.       Session: 'TyrusSession{uri=/WindTest/voiceChat/asd/username, id='6899d950-9e2e-4950-8bb8-03bd839253c0',   endpointWrapper=TyrusEndpointWrapper{endpointClass=null, endpoint=org.glassfish.tyrus.core.AnnotatedEndpoint@5bb273b4, contextPath='/WindTest'}}'.
at org.glassfish.tyrus.core.TyrusEndpointWrapper.onMessage(TyrusEndpointWrapper.java:752)
at org.glassfish.tyrus.core.TyrusWebSocket.onMessage(TyrusWebSocket.java:200)
at org.glassfish.tyrus.core.frame.TextFrame.respond(TextFrame.java:135)
at org.glassfish.tyrus.core.ProtocolHandler.process(ProtocolHandler.java:622)
at org.glassfish.tyrus.core.TyrusWebSocketEngine$TyrusReadHandler.handle(TyrusWebSocketEngine.java:394)
at org.glassfish.tyrus.servlet.TyrusHttpUpgradeHandler.onDataAvailable(TyrusHttpUpgradeHandler.java:164)
at org.apache.catalina.connector.InputBuffer$ReadHandlerImpl.processDataAvailable(InputBuffer.java:488)

How can I make audio chatting with Java(Tyrus project, websocket, Spring) Thanks in advance

0

There are 0 answers