Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //file server.js
- //-----
- var app = require('express')();
- var http = require('http').Server(app);
- var io = require('socket.io')(http);
- app.get('/', function (req, res) {
- res.send("<h1>It works!</h1>");
- });
- io.on('connection', function(socket){
- console.log('new client connected');
- socket.on('disconnet', function(){
- console.log('a client disconnect');
- })
- socket.on('notif',function(msg){
- console.log('message: '+msg.name+ ': ' + msg.message);
- io.emit('notif', {name: msg.name, message: msg.message});
- })
- });
- http.listen(3000, function(){
- console.log('listening on *:3000');
- });
- //file notif.js
- //file ini saya load di view => $this->registerJs($this->render('notif.js'), \yii\web\View::POS_READY);
- var socket = io.connect('https://chatserver.dutainformasi.net');
- socket.on('notif', function (data) {
- $( "#notifikasi" ).prepend( "<p><strong>" + data.name + "</strong>: " + data.message + "</p>" );
- $.notify({
- message: "<strong>" + data.name + "</strong>: " + data.message,
- }, {
- type: 'success',
- placement: {
- from: "bottom",
- align: "left"
- },
- })
- });
- $('#chat-form').submit(function() {
- var nama = $("#nick_name").val();
- var msg = $("#message-field").val();
- if ((nama != '') && (msg != '')){
- socket.emit('notif',{ name: nama, message: msg});
- $("#message-field").val("");
- }
- return false;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement