Skip to content

Commit

Permalink
BugFix: 소켓 에러 해결 (#66)
Browse files Browse the repository at this point in the history
BugFix: 소켓 에러 해결 (#66)
  • Loading branch information
Lee-Dahyeon authored Nov 30, 2024
2 parents f1b12f1 + df278a4 commit 5ccd7f9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import com.hyunsolution.dangu.chatRoom.domain.ChatRoom;
import com.hyunsolution.dangu.common.BaseEntity;
import com.hyunsolution.dangu.user.domain.User;
import java.awt.*;
import java.time.LocalDateTime;
import javax.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

Expand All @@ -19,6 +19,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@EntityListeners(AuditingEntityListener.class)
@Table(indexes = {@Index(name = "idx_chatRoom_id_id", columnList = "chatRoom_id, id DESC")})
@DynamicInsert
public class Chatting extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down Expand Up @@ -46,8 +47,7 @@ public class Chatting extends BaseEntity {
private LocalDateTime createdAt;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
@ColumnDefault("TEXT")
@ColumnDefault("'TEXT'")
private MessageType messageType;

@Builder
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/hyunsolution/dangu/common/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import lombok.Getter;
import org.hibernate.annotations.ColumnDefault;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@Getter
public class BaseEntity {
// 삭제
private boolean isDeleted = false;
@ColumnDefault("false")
private boolean isDeleted;

public void toggleDeleted() {
isDeleted = !isDeleted;
Expand Down
59 changes: 59 additions & 0 deletions src/main/resources/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Chat Test</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.6.1/sockjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>
</head>
<body>
<h1>Chat Test</h1>
<div id="chat">
<div id="messages"></div>
<input type="text" id="messageInput" placeholder="Type your message" />
<button id="sendBtn">Send</button>
</div>

<script>
let client;
const socket = new WebSocket('ws://15.164.186.158:8080/chat');
client = Stomp.over(socket);

// WebSocket 연결 및 STOMP 클라이언트 초기화
function connect() {
client.connect({}, function(frame) {
console.log('Connected to WebSocket server!');

// 메시지 구독
client.subscribe('/topic/chat/1', function(message) {
const receivedMessage = message.body;
const messageDiv = document.createElement('div');
messageDiv.textContent = `Received: ${receivedMessage}`;
document.getElementById('messages').appendChild(messageDiv);
});
}, function(error) {
console.error('Connection error:', error);
});
}

// 메시지 보내기
function sendMessage() {
if (!client.connected) {
console.error("STOMP client is not connected.");
return;
}
const message = document.getElementById('messageInput').value;
client.send('/app/chat/1', {
'Authorization': '1'
}, JSON.stringify({message: message}));
document.getElementById('messageInput').value = ''; // 입력창 초기화
}

// 이벤트 리스너 설정
document.getElementById('sendBtn').addEventListener('click', sendMessage);

// WebSocket 연결 시작
connect();
</script>
</body>
</html>

0 comments on commit 5ccd7f9

Please sign in to comment.