通过踩踏端点接收消息

时间:2020-07-31 09:10:56

标签: java spring spring-boot

我是Java新手。我正在尝试在Spring Boot应用程序中实现websocket。申请流程就是这样

  • 我将拨打twilio电话号码。通话接通后,twilio将向我的服务器发送一个Http GET请求。
  • 作为回应,我包括了twiML Stream 动词,它将在twilio和我的应用程序之间打开websocket,并将呼叫音频数据包发送到流动词中提到的websocket url。

我已经能够连接到twilio,但是我看不到音频数据包。

WsConfig.java

package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry; 
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;

@Configuration
@EnableWebSocketMessageBroker
public class WsConfig implements WebSocketMessageBrokerConfigurer{
    
    public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
        registration.setMessageSizeLimit(500 * 1024);
        registration.setSendBufferSizeLimit(1024 * 1024);
        registration.setSendTimeLimit(20000);
    }
    
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/transcription-socket");
    }
    
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic");
        registry.setApplicationDestinationPrefixes("/app");
    }

}

TwilioController.java

package com.example.demo;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@RestController
public class TwilioController {
    @RequestMapping(value="/twiml",produces = {"application/xml"})
    public String twiMlResponse() {
        return "<Response><Start><Stream url=\"wss://ngrok-url/transcription-socket\"></Stream></Start><Say>Please speak to start transcription.</Say><Pause length=\"60\"/></Response>";
    }
}

TwilioVoiceController.java

package com.example.demo;

import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.util.HtmlUtils;

@Controller
public class TwilioVoiceController {


  @MessageMapping("/transcription-socket")
  public void audioMessage() throws Exception {
    System.out.println("packet recieved");
  }

}

我可以看到当协议切换到101时,websocket已连接 enter image description here

0 个答案:

没有答案
相关问题