Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
mathieu
technobel.sf
Commits
eca08127
Commit
eca08127
authored
Jul 02, 2018
by
Mat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
envoie le message à tous les utilisateurs
parent
090f86d6
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
5 deletions
+40
-5
src/SocketBundle/Resources/views/Chat/message.html.twig
src/SocketBundle/Resources/views/Chat/message.html.twig
+12
-2
src/SocketBundle/Services/ChatHub.php
src/SocketBundle/Services/ChatHub.php
+28
-3
No files found.
src/SocketBundle/Resources/views/Chat/message.html.twig
View file @
eca08127
{%
extends
"base.html.twig"
%}
{%
extends
"base.html.twig"
%}
{%
block
layout
%}
{%
block
layout
%}
<section
style=
"margin: 2em;"
>
<h5>
Hello World!
Hello World!
</h5>
<input
id=
"message"
type=
"text"
>
<button
id=
"send"
>
Envoyer
</button>
</section>
{%
endblock
%}
{%
endblock
%}
{%
block
javascripts
%}
{%
block
javascripts
%}
...
@@ -16,7 +25,8 @@
...
@@ -16,7 +25,8 @@
connection
.
onopen
=
function
(
e
)
connection
.
onopen
=
function
(
e
)
{
{
connection
.
send
(
"
hello
"
);
let
message
=
$
(
'
#message
'
).
val
();
connection
.
send
(
message
);
};
};
connection
.
onmessage
=
function
(
e
)
connection
.
onmessage
=
function
(
e
)
...
...
src/SocketBundle/Services/ChatHub.php
View file @
eca08127
...
@@ -19,9 +19,21 @@ class ChatHub implements MessageComponentInterface
...
@@ -19,9 +19,21 @@ class ChatHub implements MessageComponentInterface
* et hop, il ajoute tout ce qui doit être implémenté
* et hop, il ajoute tout ce qui doit être implémenté
*/
*/
/**
* @var ContainerInterface
*/
private
$container
;
private
$container
;
/**
* @var ConnectionInterface
*/
private
$clients
;
private
$clients
;
/**
* ChatHub constructor.
*
* @param ContainerInterface $_container
*/
public
function
__construct
(
ContainerInterface
$_container
)
public
function
__construct
(
ContainerInterface
$_container
)
{
{
$this
->
container
=
$_container
;
$this
->
container
=
$_container
;
...
@@ -37,14 +49,17 @@ class ChatHub implements MessageComponentInterface
...
@@ -37,14 +49,17 @@ class ChatHub implements MessageComponentInterface
*/
*/
function
onOpen
(
ConnectionInterface
$conn
)
function
onOpen
(
ConnectionInterface
$conn
)
{
{
// TODO: Implement onOpen() method.
echo
"Un utilisateur s'est connecté
\n
"
;
echo
"Un utilisateur s'est connecté
\n
"
;
$this
->
clients
[]
=
$conn
;
$response
=
json_encode
([
$response
=
json_encode
([
'type'
=>
'msg_server'
,
'type'
=>
'msg_server'
,
'author'
=>
'server'
,
'author'
=>
'server'
,
'content'
=>
"Bienvenue sur notre Web Socket Server"
,
'content'
=>
"Bienvenue sur notre Web Socket Server"
,
'timestamp'
=>
(
new
\
DateTime
())
->
getTimestamp
()
'timestamp'
=>
(
new
\
DateTime
())
->
getTimestamp
()
]);
]);
$conn
->
send
(
$response
);
$conn
->
send
(
$response
);
}
}
...
@@ -83,6 +98,16 @@ class ChatHub implements MessageComponentInterface
...
@@ -83,6 +98,16 @@ class ChatHub implements MessageComponentInterface
*/
*/
function
onMessage
(
ConnectionInterface
$from
,
$msg
)
function
onMessage
(
ConnectionInterface
$from
,
$msg
)
{
{
// TODO: Implement onMessage() method.
echo
"Le message "
.
$msg
.
" a été reçu
\n
"
;
echo
"Le message "
.
$msg
.
" a été reçu
\n
"
;
}}
foreach
(
$this
->
clients
as
$client
)
\ No newline at end of file
{
$response
=
json_encode
([
'type'
=>
'msg_from_user'
,
'author'
=>
""
,
'content'
=>
$msg
,
'timestamp'
=>
(
new
\
DateTime
())
->
getTimestamp
()
]);
$client
->
send
(
$response
);
}
}
}
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