如何为websocket添加端口?

时间:2019-04-12 11:50:00

标签: spring websocket

我将websocket添加到了我的应用程序中。要检查它是否正常工作,我必须向端点发出请求,所以这是问题所在,如何添加端口进行连接?

public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/ws://localhost:9009");
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.setApplicationDestinationPrefixes("/app");
    registry.enableSimpleBroker("/topic");
}

}

public class ChatController {
@Autowired
PlanfixService planfixService;

@Autowired
UserService userService;

@MessageMapping("/messageToPlanfix")
@SendTo("/topic/public")
public void sendMessageToPlanfix(@RequestBody PlanfixModel planfixModel) throws IOException, URISyntaxException {
    User dbUser = userService.findUserByUsername(AuthenticationController.selfUserName());
    planfixModel.setContactId(String.valueOf(dbUser.getId()));
    planfixModel.setContactName(dbUser.getFirstName());
    planfixService.save(planfixModel);
    requestToPlanfix(planfixModel);
}

public void requestToPlanfix(@RequestBody PlanfixModel planfixModel) throws IOException, URISyntaxException {
    String postUrl = "https://womanmarathon.planfix.ru/webchat/api/";
    Gson gson = new Gson();
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(postUrl);
    StringEntity postingString = new StringEntity(gson.toJson(planfixModel));//gson.tojson() converts your pojo to json
    post.setEntity(postingString);
    post.setHeader("Content-type", "application/json");
    HttpResponse response = httpClient.execute(post);
}


@RequestMapping(value = "/requestFromPlanfix", method = RequestMethod.POST)
@MessageMapping("/messageFromPlanfix")
@SendTo("/topic/public")
public PlanfixModel requestFromPlanfix(@RequestBody PlanfixModel planfixModel) throws Exception {
    planfixService.save(planfixModel);
    return planfixModel;
}

需要通过application.properties添加端口吗?

}

0 个答案:

没有答案