-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e54b99
commit 9b6c8c0
Showing
3 changed files
with
110 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/solution/gdsc/PathPal/global/config/WebSocketConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package solution.gdsc.PathPal.global.config; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocket; | ||
import org.springframework.web.socket.config.annotation.WebSocketConfigurer; | ||
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; | ||
|
||
@Configuration | ||
@EnableWebSocket | ||
public class WebSocketConfiguration implements WebSocketConfigurer { | ||
|
||
@Autowired | ||
private WebSocketHandler webSocketHandler; | ||
|
||
@Override | ||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { | ||
registry.addHandler(webSocketHandler, "/socket") // Handshake 주소 | ||
.setAllowedOrigins("*"); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/solution/gdsc/PathPal/global/config/WebSocketHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package solution.gdsc.PathPal.global.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.socket.BinaryMessage; | ||
import org.springframework.web.socket.CloseStatus; | ||
import org.springframework.web.socket.TextMessage; | ||
import org.springframework.web.socket.WebSocketSession; | ||
import org.springframework.web.socket.handler.BinaryWebSocketHandler; | ||
|
||
import javax.imageio.stream.FileImageOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
@Component | ||
public class WebSocketHandler extends BinaryWebSocketHandler { | ||
|
||
@Value("${image.path}") | ||
private String path; | ||
|
||
private final Set<WebSocketSession> sessions = ConcurrentHashMap.newKeySet(); | ||
|
||
private final AtomicInteger imageNumber = new AtomicInteger(1); | ||
|
||
@Override | ||
public void afterConnectionEstablished(WebSocketSession session) throws Exception { | ||
session.setBinaryMessageSizeLimit(1024 * 1024 * 10); | ||
sessions.add(session); | ||
} | ||
|
||
@Override | ||
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception { | ||
byte[] bytes = message.getPayload().array(); | ||
|
||
String responseMessage = ""; | ||
try { | ||
int imageId = imageNumber.get(); | ||
String fileFullName = path + imageId + ".jpeg"; | ||
System.out.println(fileFullName); | ||
|
||
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(fileFullName)); | ||
imageOutput.write(bytes, 0, bytes.length); | ||
imageOutput.close(); | ||
responseMessage = "성공적으로 저장했습니다. 받은 바이트 크기=" + bytes.length + ", savedImageId=" + imageId; | ||
imageNumber.incrementAndGet(); | ||
} catch (Exception e) { | ||
responseMessage = "저장에 실패했습니다. 받은 바이트 크기=" + bytes.length; | ||
} | ||
try { | ||
session.sendMessage(new TextMessage(responseMessage)); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { | ||
sessions.remove(session); | ||
} | ||
} |