From b1c501e2f239ef6dcbf5e55e403834d793c6a0f6 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sat, 30 Jun 2018 23:44:50 +0200 Subject: Finished websocket integration --- resources/assets/js/app.js | 15 +---- resources/assets/js/bootstrap.js | 22 +++---- resources/assets/js/chat.js | 26 +++++++++ resources/assets/js/chatServer.js | 26 +++++++++ resources/assets/sass/app.scss | 14 +++-- resources/views/auth/login.blade.php | 69 ++++++++++++++++++++++ resources/views/auth/passwords/email.blade.php | 47 +++++++++++++++ resources/views/auth/passwords/reset.blade.php | 65 +++++++++++++++++++++ resources/views/auth/register.blade.php | 77 +++++++++++++++++++++++++ resources/views/layouts/app.blade.php | 79 ++++++++++++++++++++++++++ resources/views/welcome.blade.php | 27 --------- resources/views/writeMessage.blade.php | 33 +++++++++++ 12 files changed, 441 insertions(+), 59 deletions(-) create mode 100644 resources/assets/js/chat.js create mode 100644 resources/assets/js/chatServer.js create mode 100644 resources/views/auth/login.blade.php create mode 100644 resources/views/auth/passwords/email.blade.php create mode 100644 resources/views/auth/passwords/reset.blade.php create mode 100644 resources/views/auth/register.blade.php create mode 100644 resources/views/layouts/app.blade.php delete mode 100644 resources/views/welcome.blade.php create mode 100644 resources/views/writeMessage.blade.php (limited to 'resources') diff --git a/resources/assets/js/app.js b/resources/assets/js/app.js index 98eca79..8400475 100644 --- a/resources/assets/js/app.js +++ b/resources/assets/js/app.js @@ -1,4 +1,3 @@ - /** * First we will load all of this project's JavaScript dependencies which * includes Vue and other libraries. It is a great starting point when @@ -6,17 +5,7 @@ */ require('./bootstrap'); - window.Vue = require('vue'); +// window.jQueryMigrate = require('jquery-migrate'); +require('./chat'); -/** - * Next, we will create a fresh Vue application instance and attach it to - * the page. Then, you may begin adding components to this application - * or customize the JavaScript scaffolding to fit your unique needs. - */ - -Vue.component('example-component', require('./components/ExampleComponent.vue')); - -const app = new Vue({ - el: '#app' -}); diff --git a/resources/assets/js/bootstrap.js b/resources/assets/js/bootstrap.js index fb0f1ed..a919262 100644 --- a/resources/assets/js/bootstrap.js +++ b/resources/assets/js/bootstrap.js @@ -1,4 +1,3 @@ - window._ = require('lodash'); window.Popper = require('popper.js').default; @@ -10,7 +9,6 @@ window.Popper = require('popper.js').default; try { window.$ = window.jQuery = require('jquery'); - require('bootstrap'); } catch (e) {} @@ -43,14 +41,12 @@ if (token) { * for events that are broadcast by Laravel. Echo and event broadcasting * allows your team to easily build robust real-time web applications. */ - -// import Echo from 'laravel-echo' - -// window.Pusher = require('pusher-js'); - -// window.Echo = new Echo({ -// broadcaster: 'pusher', -// key: process.env.MIX_PUSHER_APP_KEY, -// cluster: process.env.MIX_PUSHER_APP_CLUSTER, -// encrypted: true -// }); +// import Echo from "laravel-echo" +// window.io = require('socket.io-client'); + +// if (typeof io !== 'undefined') { +// window.Echo = new Echo({ +// broadcaster: 'socket.io', +// host: window.location.hostname + ':6001', +// }); +// } \ No newline at end of file diff --git a/resources/assets/js/chat.js b/resources/assets/js/chat.js new file mode 100644 index 0000000..5f634d2 --- /dev/null +++ b/resources/assets/js/chat.js @@ -0,0 +1,26 @@ +var socket = io('http://127.0.0.1:8890', { + transports: ['websocket'] +}); +socket.on('message', function (data) { + data = JSON.parse(data); + $("#messages").append("
" + data.user + " : " + data.message + "
"); +}); + +$('input.send').click(function (e) { + e.preventDefault(); + sendMessage(); +}); + +function sendMessage() { + var message = $('input.message').val(); + $.ajax({ + type: "POST", + url: "sendMessage", + data: { + "_token": $('meta[name="csrf-token"]').attr('content'), + "message": message + }, + cache: false, + success: function (results) {} + }); +} \ No newline at end of file diff --git a/resources/assets/js/chatServer.js b/resources/assets/js/chatServer.js new file mode 100644 index 0000000..fb6c7fc --- /dev/null +++ b/resources/assets/js/chatServer.js @@ -0,0 +1,26 @@ +var app = require('express')(); +var server = require('http').Server(app); +var io = require('socket.io')(server); +var redis = require('redis'); +var port = 8890; + +server.listen(port, function () { + console.log("Listening on " + port) +}); + +io.on('connection', function (socket) { + + console.log("new client connected"); + var redisClient = redis.createClient(); + redisClient.subscribe('message'); + + redisClient.on("message", function (channel, message) { + console.log("new message" + message); + socket.emit(channel, message); + }); + + socket.on('disconnect', function () { + console.log("client connected"); + redisClient.quit(); + }); +}); diff --git a/resources/assets/sass/app.scss b/resources/assets/sass/app.scss index 0077cb1..c58e2db 100644 --- a/resources/assets/sass/app.scss +++ b/resources/assets/sass/app.scss @@ -1,14 +1,16 @@ - // Fonts @import url("https://fonts.googleapis.com/css?family=Raleway:300,400,600"); - // Variables @import "variables"; - // Bootstrap @import '~bootstrap/scss/bootstrap'; - .navbar-laravel { - background-color: #fff; - box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04); + background-color: #fff; + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04); } + +.MessagesWindow { + height: 250px; + overflow-y: scroll; + padding: 15px; +} \ No newline at end of file diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php new file mode 100644 index 0000000..9fd12a7 --- /dev/null +++ b/resources/views/auth/login.blade.php @@ -0,0 +1,69 @@ +@extends('layouts.app') + +@section('content') +
- Try publishing an event to channel my-channel
- with event name my-event
.
-
{{$message->name}}: {{$message->message}}
+ @endforeach + +