souravndp

Untitled

Nov 11th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. ===============================SERVER SIDE========================================
  2.  
  3. var PORT = process.env.OPENSHIFT_INTERNAL_PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;
  4. var IPADDRESS = process.env.OPENSHIFT_INTERNAL_IP || process.env.OPENSHIFT_NODEJS_IP || 'localhost';
  5. var express = require('express');
  6. var app = express();
  7. var server = require('http').createServer(app);
  8. server.listen(PORT, IPADDRESS,function(){
  9. console.log("Server listen in " + IPADDRESS + ":" + PORT);
  10. });
  11. var io = require('socket.io')(server,{origins:'*:*'});
  12. io.sockets.on('connection', function (socket) {
  13. socket.emit('message', { message: 'welcome to the chat' });
  14. socket.on('send', function (data) {
  15. io.sockets.emit('message', data);
  16. });
  17. });
  18.  
  19.  
  20.  
  21. ===============================CLIENT SIDE========================================
  22.  
  23.  
  24. <html>
  25. <head>
  26. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  27. <script src="https://cdn.socket.io/socket.io-1.4.8.js"></script>
  28. <script>
  29. $(document).ready(function(){
  30. if ('speechSynthesis' in window) {
  31. var messages = [];
  32. //var socket = io.connect("nodejs1-souravg.rhcloud.com:8000/");
  33. var socket = new io('nodejs1-souravg.rhcloud.com:8000/',{'forceNew':true});
  34. var field = document.getElementById("field");
  35. var sendButton = document.getElementById("send");
  36. var content = document.getElementById("content");
  37. socket.on('message', function (data) {
  38. if(data.message) {
  39. messages.push(data.message);
  40. var html = '';
  41. for(var i=0; i<messages.length; i++) {
  42. html += messages[i] + '<br />';
  43. }
  44. content.innerHTML = html;
  45. onloadVoice(data.message);
  46. } else {
  47. console.log("There is a problem:", data);
  48. }
  49. });
  50. sendButton.onclick = function() {
  51. var text = field.value;
  52. socket.emit('send', { message: text });
  53. };
  54. onloadVoice = function(message) {
  55. var msg = new SpeechSynthesisUtterance(message);
  56. window.speechSynthesis.speak(msg);
  57. }
  58. }
  59. else {
  60. alert("Sorry, your browser is not support for read text")
  61. }
  62.  
  63. });
  64.  
  65. </script>
  66. </head>
  67. <body>
  68. <div id="content" style="width: 500px; height: 300px; margin: 0 0 20px 0; border: solid 1px #999; overflow-y: scroll;"></div>
  69. <div class="controls">
  70. <input style="width:350px;" id="field" class="field">
  71. <input type="button" value="send" id="send" class="send">
  72. </div>
  73.  
  74. </body>
  75.  
  76. </html>
Add Comment
Please, Sign In to add comment