Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
SkuciSe
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tim 2 - 2022
SkuciSe
Commits
5aa65891
Commit
5aa65891
authored
Sep 17, 2022
by
Milovan Samardzic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WebSocketConfig
parent
fd4f4f00
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
177 additions
and
0 deletions
+177
-0
SkuciSe/pom.xml
+4
-0
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSocket/Message.java
+14
-0
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSocket/MessageController.java
+23
-0
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSocket/ResponseMessage.java
+14
-0
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSocket/UserHandshakeHandler.java
+20
-0
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSocket/WSController.java
+20
-0
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSocket/WSService.java
+21
-0
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSocket/WebSocketConfig.java
+23
-0
SkuciSe/src/main/java/com/example/SkuciSe/controller/AppController.java
+8
-0
SkuciSe/src/main/resources/static/js/socket.js
+30
-0
SkuciSe/src/main/resources/templates/chat.html
+0
-0
No files found.
SkuciSe/pom.xml
View file @
5aa65891
...
@@ -60,6 +60,10 @@
...
@@ -60,6 +60,10 @@
<artifactId>
spring-security-test
</artifactId>
<artifactId>
spring-security-test
</artifactId>
<scope>
test
</scope>
<scope>
test
</scope>
</dependency>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-websocket
</artifactId>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSocket/Message.java
0 → 100644
View file @
5aa65891
package
com
.
example
.
SkuciSe
.
configuration
.
WebSocket
;
import
lombok.AllArgsConstructor
;
import
lombok.Getter
;
import
lombok.NoArgsConstructor
;
import
lombok.Setter
;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public
class
Message
{
private
String
messageContent
;
}
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSocket/MessageController.java
0 → 100644
View file @
5aa65891
package
com
.
example
.
SkuciSe
.
configuration
.
WebSocket
;
import
com.example.SkuciSe.model.korisnik.Korisnik
;
import
org.springframework.messaging.handler.annotation.MessageMapping
;
import
org.springframework.messaging.simp.annotation.SendToUser
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.util.HtmlUtils
;
import
java.security.Principal
;
@Controller
public
class
MessageController
{
@MessageMapping
(
"/private-message"
)
@SendToUser
(
"topic/private-messages"
)
public
ResponseMessage
getPrivateMessage
(
final
Message
message
,
final
Principal
principal
)
throws
InterruptedException
{
Thread
.
sleep
(
1000
);
return
new
ResponseMessage
(
HtmlUtils
.
htmlEscape
(
"Sending private message to user "
+
principal
.
getName
()
+
": "
+
message
.
getMessageContent
())
);
}
}
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSocket/ResponseMessage.java
0 → 100644
View file @
5aa65891
package
com
.
example
.
SkuciSe
.
configuration
.
WebSocket
;
import
lombok.AllArgsConstructor
;
import
lombok.Getter
;
import
lombok.NoArgsConstructor
;
import
lombok.Setter
;
@AllArgsConstructor
@Getter
@Setter
@NoArgsConstructor
public
class
ResponseMessage
{
private
String
content
;
}
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSocket/UserHandshakeHandler.java
0 → 100644
View file @
5aa65891
package
com
.
example
.
SkuciSe
.
configuration
.
WebSocket
;
import
com.sun.security.auth.UserPrincipal
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.http.server.ServerHttpRequest
;
import
org.springframework.security.core.context.SecurityContextHolder
;
import
org.springframework.web.socket.WebSocketHandler
;
import
org.springframework.web.socket.server.support.DefaultHandshakeHandler
;
import
java.security.Principal
;
import
java.util.Map
;
public
class
UserHandshakeHandler
extends
DefaultHandshakeHandler
{
private
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
UserHandshakeHandler
.
class
);
@Override
protected
Principal
determineUser
(
ServerHttpRequest
request
,
WebSocketHandler
wsHandler
,
Map
<
String
,
Object
>
attributes
)
{
return
new
UserPrincipal
(
SecurityContextHolder
.
getContext
().
getAuthentication
().
getName
());
}
}
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSocket/WSController.java
0 → 100644
View file @
5aa65891
package
com
.
example
.
SkuciSe
.
configuration
.
WebSocket
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RestController
;
@RestController
public
class
WSController
{
@Autowired
private
WSService
service
;
@PostMapping
(
"/send-private-message/{mail}"
)
public
void
sendPrivateMessage
(
@PathVariable
(
"mail"
)
final
String
mail
,
@RequestBody
final
Message
message
)
{
service
.
notifyUser
(
mail
,
message
.
getMessageContent
());
}
}
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSocket/WSService.java
0 → 100644
View file @
5aa65891
package
com
.
example
.
SkuciSe
.
configuration
.
WebSocket
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.messaging.simp.SimpMessagingTemplate
;
import
org.springframework.stereotype.Service
;
@Service
public
class
WSService
{
private
final
SimpMessagingTemplate
messagingTemplate
;
@Autowired
public
WSService
(
SimpMessagingTemplate
messagingTemplate
)
{
this
.
messagingTemplate
=
messagingTemplate
;
}
public
void
notifyUser
(
final
String
mail
,
final
String
message
)
{
ResponseMessage
response
=
new
ResponseMessage
(
message
);
messagingTemplate
.
convertAndSendToUser
(
mail
,
"/topic/private-messages"
,
response
);
}
}
\ No newline at end of file
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSocket/WebSocketConfig.java
0 → 100644
View file @
5aa65891
package
com
.
example
.
SkuciSe
.
configuration
.
WebSocket
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.messaging.simp.config.MessageBrokerRegistry
;
import
org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker
;
import
org.springframework.web.socket.config.annotation.StompEndpointRegistry
;
import
org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer
;
@Configuration
@EnableWebSocketMessageBroker
public
class
WebSocketConfig
implements
WebSocketMessageBrokerConfigurer
{
@Override
public
void
configureMessageBroker
(
MessageBrokerRegistry
config
)
{
config
.
enableSimpleBroker
(
"/topic"
);
config
.
setApplicationDestinationPrefixes
(
"/ws"
);
}
@Override
public
void
registerStompEndpoints
(
StompEndpointRegistry
registry
)
{
registry
.
addEndpoint
(
"out-websocket"
)
.
withSockJS
();
}
}
SkuciSe/src/main/java/com/example/SkuciSe/controller/AppController.java
View file @
5aa65891
package
com
.
example
.
SkuciSe
.
controller
;
package
com
.
example
.
SkuciSe
.
controller
;
import
com.example.SkuciSe.configuration.EmailPostoji
;
import
com.example.SkuciSe.configuration.EmailPostoji
;
import
com.example.SkuciSe.configuration.WebSocket.Message
;
import
com.example.SkuciSe.model.korisnik.Korisnik
;
import
com.example.SkuciSe.model.korisnik.Korisnik
;
import
com.example.SkuciSe.model.korisnik.KorisnikDetails
;
import
com.example.SkuciSe.model.korisnik.KorisnikDetails
;
import
com.example.SkuciSe.model.oglas.Oglas
;
import
com.example.SkuciSe.model.oglas.Oglas
;
...
@@ -8,12 +9,14 @@ import com.example.SkuciSe.repository.KorisnikRepository;
...
@@ -8,12 +9,14 @@ import com.example.SkuciSe.repository.KorisnikRepository;
import
com.example.SkuciSe.repository.LokacijaRepository
;
import
com.example.SkuciSe.repository.LokacijaRepository
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.security.core.annotation.AuthenticationPrincipal
;
import
org.springframework.security.core.annotation.AuthenticationPrincipal
;
import
org.springframework.security.core.context.SecurityContextHolder
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.security.Principal
;
import
java.util.Base64
;
import
java.util.Base64
;
@Controller
@Controller
...
@@ -78,4 +81,9 @@ public class AppController
...
@@ -78,4 +81,9 @@ public class AppController
model
.
addAttribute
(
"loggedUser"
,
loggedUser
);
model
.
addAttribute
(
"loggedUser"
,
loggedUser
);
return
"portfolio"
;
return
"portfolio"
;
}
}
@GetMapping
(
"/chat"
)
public
String
getChat
(
Model
model
,
@AuthenticationPrincipal
KorisnikDetails
loggedUser
){
model
.
addAttribute
(
"loggedUser"
,
loggedUser
);
return
"chat"
;
}
}
}
SkuciSe/src/main/resources/static/js/socket.js
0 → 100644
View file @
5aa65891
var
stompClient
=
null
;
$
(
document
).
ready
(
function
()
{
console
.
log
(
"Index page is ready"
);
connect
();
$
(
"#send-private"
).
click
(
function
()
{
sendPrivateMessage
();
});
});
function
connect
()
{
var
socket
=
new
SockJS
(
'out-websocket'
);
stompClient
=
Stomp
.
over
(
socket
);
stompClient
.
connect
({},
function
(
frame
)
{
console
.
log
(
'Connected: '
+
frame
);
stompClient
.
subscribe
(
'/user/topic/private-messages'
,
function
(
message
)
{
showMessage
(
JSON
.
parse
(
message
.
body
).
content
);
});
});
}
function
showMessage
(
message
)
{
$
(
"#messages"
).
append
(
"<tr><td>"
+
message
+
"</td></tr>"
);
}
function
sendPrivateMessage
()
{
console
.
log
(
"sending private message"
);
stompClient
.
send
(
"/ws/private-message"
,
{},
JSON
.
stringify
({
'messageContent'
:
$
(
"#private-message"
).
val
()}));
}
\ No newline at end of file
SkuciSe/src/main/resources/templates/chat.html
0 → 100644
View file @
5aa65891
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment