Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ===============================SERVER SIDE========================================
- var PORT = process.env.OPENSHIFT_INTERNAL_PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;
- var IPADDRESS = process.env.OPENSHIFT_INTERNAL_IP || process.env.OPENSHIFT_NODEJS_IP || 'localhost';
- var express = require('express');
- var app = express();
- var server = require('http').createServer(app);
- server.listen(PORT, IPADDRESS,function(){
- console.log("Server listen in " + IPADDRESS + ":" + PORT);
- });
- var io = require('socket.io')(server,{origins:'*:*'});
- io.sockets.on('connection', function (socket) {
- socket.emit('message', { message: 'welcome to the chat' });
- socket.on('send', function (data) {
- io.sockets.emit('message', data);
- });
- });
- ===============================CLIENT SIDE========================================
- <html>
- <head>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
- <script src="https://cdn.socket.io/socket.io-1.4.8.js"></script>
- <script>
- $(document).ready(function(){
- if ('speechSynthesis' in window) {
- var messages = [];
- //var socket = io.connect("nodejs1-souravg.rhcloud.com:8000/");
- var socket = new io('nodejs1-souravg.rhcloud.com:8000/',{'forceNew':true});
- var field = document.getElementById("field");
- var sendButton = document.getElementById("send");
- var content = document.getElementById("content");
- socket.on('message', function (data) {
- if(data.message) {
- messages.push(data.message);
- var html = '';
- for(var i=0; i<messages.length; i++) {
- html += messages[i] + '<br />';
- }
- content.innerHTML = html;
- onloadVoice(data.message);
- } else {
- console.log("There is a problem:", data);
- }
- });
- sendButton.onclick = function() {
- var text = field.value;
- socket.emit('send', { message: text });
- };
- onloadVoice = function(message) {
- var msg = new SpeechSynthesisUtterance(message);
- window.speechSynthesis.speak(msg);
- }
- }
- else {
- alert("Sorry, your browser is not support for read text")
- }
- });
- </script>
- </head>
- <body>
- <div id="content" style="width: 500px; height: 300px; margin: 0 0 20px 0; border: solid 1px #999; overflow-y: scroll;"></div>
- <div class="controls">
- <input style="width:350px;" id="field" class="field">
- <input type="button" value="send" id="send" class="send">
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment