gpt4 book ai didi

javascript - 使用 websocket 将客户端连接到服务器

转载 作者:行者123 更新时间:2023-12-01 16:31:51 25 4
gpt4 key购买 nike

我使用 websocket 技术开发了一个小型聊天应用程序,但在将浏览器连接到服务器时遇到了一些问题。

更新 19:42我修复了使用丢失的 JS 的连接问题:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.1.4/sockjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>

但是我有新的麻烦了。消息不显示在客户端..

带有 JS 的 Index.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

<!DOCTYPE HTML>
<html>
<head>
<title>Главная</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="${contextPath}/resources/css/style.css">

<script type="text/javascript">
var stompClient = null;
function setConnected(connected) {
document.getElementById('connect').disabled = connected;
document.getElementById('disconnect').disabled = !connected;
document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
document.getElementById('response').innerHTML = '';
}
function connect() {
var socket = new SockJS('/chat');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/chating', function(message){
showGreeting(JSON.parse(chating.body).content);
});
});
}
function disconnect() {
stompClient.disconnect();
setConnected(false);
console.log("Disconnected");
}
function sendMessage() {
var message = document.getElementById('message').value;
stompClient.send("/app/chat", {}, JSON.stringify(message));
}
function showMessage(message) {
var resp = document.getElementById('messages');
resp.value = resp.value + message.data + "\n";
}
</script>

</head>
<body>
<div class="center">
<div>
<sec:authorize access="!isAuthenticated()">
<a href="/login"><button>Войти</button></a>
<a href="/registration"><button>Зарегистрироваться</button></a>
</sec:authorize>
<sec:authorize access="isAuthenticated()">
<p>Привет, <b>${pageContext.request.userPrincipal.name}</b>!</p>
<a href="/logout"><button>Выйти</button></a>
<button id="connect" onclick="connect();">Соединение</button>
</sec:authorize>
</div>
<div id="chatbox">
<textarea id="messages" rows="20" cols="50" readonly="readonly"></textarea>
</div>
<sec:authorize access="isAuthenticated()">
<form name="message" action="">
<input name="usermsg" type="text" id="message" size="40"/>
<input type="button" name="submitmsg" value="Send..." onclick="sendMessage();"/>
</form>
</sec:authorize>
</div>
</body>
</html>

消息 Controller 类

import avi.com.vk.Message;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;

@Controller
public class MessageController {

@MessageMapping("/chat")
@SendTo("topic/chating")
public Message message(String message) {
return new Message(message);
}
}

Web套接字配置类

package avi.com.vk.config;

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

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chat").withSockJS();
}

}

Link for full-project at GitHub

最佳答案

我修复了使用丢失的 JS 的连接问题:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.1.4/sockjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>

关于javascript - 使用 websocket 将客户端连接到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62023309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com