Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.35 KB | None | 0 0
  1. //create a new instance of ChatEngine
  2. ChatEngine = ChatEngineCore.create({
  3. publishKey: key
  4. subscribeKey: key
  5. },{ debug: true});
  6.  
  7. // create a bucket to store our ChatEngine Chat object
  8. let myChat;
  9.  
  10. // create a bucket to store
  11. let me;
  12.  
  13. // these function is used for typing indicator
  14. let sendmsg = function () {};
  15. let keypress = function () {};
  16.  
  17. // create an optional config object to increase the default
  18. timeout from 1000ms
  19. let typingconfig = { timeout: 1000 };
  20.  
  21. // compile handlebars templates and store them for use later
  22. let peopleTemplate = Handlebars.compile($("#person-
  23. template").html());
  24. let meTemplate = Handlebars.compile($("#message-template").html());
  25. let userTemplate = Handlebars.compile($("#message-response-
  26. template").html());
  27.  
  28. let searchconfig = { prop: 'state.full', caseSensitive: false }
  29. // this is our main function that starts our chat app
  30. const init = () => {
  31. ChatEngine.connect ('email', { username: 'name', full:'K' },
  32. 'auth-key');
  33.  
  34. // when ChatEngine is booted, it returns your new User as
  35. `data.me`
  36. here my function is called by the pubnub
  37.  
  38. ChatEngine.on('$.ready', function(data) {
  39. // store my new user as `me`
  40. me = data.me;
  41. //create a new ChatEngine Chat and I am connecting to chatenging
  42. using this option.
  43. myChat = new ChatEngine.Chat('chat-engine-server',true);
  44.  
  45. // here i am updating the the state of the user
  46. me.update(
  47. {
  48. full:'John Doe',
  49. username: 'John',
  50. uuid: 'johndoe@gmail.com'
  51. });
  52. //starting private chat logic
  53. // this is what I wrote the code that use to create the privat
  54. chat
  55.  
  56. myChat.invite(' invited email');
  57.  
  58. me.direct.on('$.invite', (payload) => {
  59. console.log("invited user");
  60. let secretChat = new ChatEngine.Chat(payload.data.channel);
  61. document.getElementById("punlicLog").style.display = 'none';
  62. document.getElementById("privateLog").style.display = 'inline';
  63. document.getElementById("message-to-send").style.display='none';
  64. document.getElementById("private-message-to-send").
  65. style.display='inline';
  66. here I am sending message to this payload
  67. secretChat.on('message', (payload) => {
  68. console.log(payload);
  69. // using this methd i am rendering private message
  70. privaterenderMessage(payload);
  71. });
  72. //$('#privateLog').append("Now you are in a Private Chat with "
  73. + globalUsr );
  74.  
  75. // this is take the message from the input box to send
  76. $("#privateMessage").keypress(function (event) {
  77. if (event.which == 13) {
  78. secretChat.emit('message', {
  79. text: $('#privateMessage').val()
  80. });
  81. $("#privateMessage").val('');
  82. event.preventDefault();
  83. }
  84. }); });
  85.  
  86. //ending private chat message
  87.  
  88. // this part belong to the pic
  89. console.log("before gravator'''''''''''''''''': ")
  90. user = new ChatEngine.User(me.state.username, {email:
  91. me.state.uuid});
  92. console.log(" full name : "+me.state.full);
  93. $("#pic").attr("src", user.state.gravatar);
  94.  
  95. myChat.on('message', (message) => {
  96. console.log("message send mychat.on() method to send
  97. ");
  98. renderMessage(message);
  99. });
  100. // when a user comes online, render them in the online list
  101. });
  102.  
  103. bind our send button and return key to send message
  104. $('#sendMessage').on('submit', sendMessage)
  105.  
  106. });
  107.  
  108. };
  109.  
  110. send a message to the Chat
  111. const sendMessage = () => {
  112.  
  113. get the message text from the text input
  114. let message = $('#message-to-send').val().trim();
  115.  
  116. if the message isn't empty
  117. if (message.length) {
  118.  
  119. emit the message event to everyone in the Chat
  120. myChat.emit('message', {
  121. text: message
  122. });
  123.  
  124. // clear out the text input
  125. $('#message-to-send').val('');
  126.  
  127. }
  128.  
  129. // stop form submit from bubbling
  130. return false;
  131.  
  132. };
  133.  
  134. // render messages in the list
  135. const renderMessage = (message, isHistory = false) => {
  136.  
  137. // use the generic user template by default
  138. let template = userTemplate;
  139.  
  140. // if I happened to send the message, use the special
  141. template for myself
  142. if (message.sender.uuid == me.uuid) {
  143. template = meTemplate;
  144. }
  145.  
  146. let el = template({
  147. messageOutput: message.data.text,
  148. time: getCurrentTime(),
  149. user: message.sender.state
  150. });
  151.  
  152. // render the message
  153. if(isHistory) {
  154. $('.chat-history ul').prepend(el);
  155. } else {
  156. $('.chat-history ul').append(el);
  157. }
  158.  
  159. // scroll to the bottom of the chat
  160. scrollToBottom();
  161. };
  162. // end of render message
  163.  
  164. const privaterenderMessage = (message, isHistory = false) => {
  165.  
  166. // use the generic user template by default
  167. let template = userTemplate;
  168.  
  169. // if I happened to send the message, use the special
  170. template for myself
  171. if (message.sender.uuid == me.uuid) {
  172. template = meTemplate;
  173. }
  174.  
  175. let el = template({
  176. messageOutput: message.data.text,
  177. time: getCurrentTime(),
  178. user: message.sender.state
  179. });
  180.  
  181. init();
  182.  
  183.  
  184.  
  185. This is my Html Page to display the chat messages to the user
  186. <!DOCTYPE html>
  187.  
  188. <body>
  189. <div class="container clearfix">
  190. <div class="people-list" id="people-list">
  191. <input type="text" id="search-user"
  192. placeholder="Search user">
  193. <ul class="list">
  194. </ul>
  195. </div>
  196.  
  197. <div class="chat">
  198.  
  199. <div class="chat-header clearfix">
  200. <!-- <img src="" alt="avatar" /> -->
  201. <div class="chat-about">
  202. <div class="chat-with">ChatEngine Demo
  203. Chat</div>
  204. </div>
  205. </div>
  206.  
  207. here i am display ing globle chat messaage
  208. <div class="chat-history " id="punlicLog">
  209. <ul></ul>
  210. </div>
  211.  
  212. here I will display the private chat message of the
  213. user
  214. <div class="private-chat-history" id="privateLog"
  215.  
  216. style="display: none">
  217. <ul></ul>
  218. </div>
  219.  
  220. <span class="badge badge-pill badge-success"
  221. id="typing" style="color:green"></span>
  222.  
  223. <form id="sendMessage" class="chat-message clearfix">
  224. <input type="text" name="message-to-send" id="message-
  225. to-send"
  226. placeholder="Type your message" rows="1"
  227. onkeypress="keypress(event)"></input>
  228. <input type="text" name="message-to-send"
  229. class=
  230. "form-control" id="private-message-to-send" style="display: none"
  231. placeholder
  232. ="Your message here..." onkeypress="keypress(event)">
  233. <input type="submit" value="Send" >
  234. </form>
  235. <!-- end chat-message -->
  236.  
  237. </div>
  238.  
  239. <!-- end chat -->
  240. </div>
  241. <!-- end container -->
  242.  
  243. <!-- dynamic message display using javascript with the
  244. pubnub -->
  245. <script id="message-template" type="text/x-handlebars-
  246. template">
  247. <li class="clearfix">
  248. <div class="message-data align-right">
  249. <span class="message-data-time">{{time}},
  250. Today</span> &nbsp; &nbsp;
  251. <span class="message-data-name">
  252. {{user.first}}</span> <i class="fa fa-circle me"></i>
  253. </div>
  254. <div class="message other-message float-right">
  255. {{messageOutput}}
  256. </div>
  257. </li>
  258. </script>
  259. <script id="message-response-template" type="text/x-
  260. handlebars-
  261. template">
  262. <li>
  263. <div class="message-data">
  264. <span class="message-data-name"><i class="fa
  265. fa-
  266. circle online"></i> {{user.first}}</span>
  267. <span class="message-data-time">{{time}},
  268. Today</span>
  269. </div>
  270. <div class="message my-message">
  271. {{messageOutput}}
  272. </div>
  273. </li>
  274. </script>
  275.  
  276.  
  277. <!-- // starting private message rendering -->
  278.  
  279.  
  280. <script id="private-message-template" type="text/x-
  281. handlebars-template" style="display: none">
  282. <li class="clearfix">
  283. <div class="message-data align-right">
  284. <span class="message-data-time">{{time}},
  285. Today</span> &nbsp; &nbsp;
  286. <span class="message-data-name">{{user.first}}
  287. </span> <i class="fa fa-circle me"></i>
  288. </div>
  289. <div class="message other-message float-right">
  290. {{messageOutput}}
  291. </div>
  292. </li>
  293. </script>
  294. <script id="private-message-response-template" type="text/x-
  295. handlebars-template" >
  296. <li>
  297. <div class="message-data">
  298. <span class="message-data-name"><i class="fa fa-
  299. circle online"></i> {{user.first}}</span>
  300. <span class="message-data-time">{{time}},
  301. Today</span>
  302. </div>
  303. <div class="message my-message">
  304. {{messageOutput}}
  305. </div>
  306. </li>
  307. </script>
  308. <!-- // ending private message rendering -->
  309.  
  310.  
  311. <script id="person-template" type="text/x-handlebars-template">
  312. {{#if state.full}}
  313. <li class="clearfix" id="{{uuid}}">
  314. <img src="" alt="photo"id="pic" style="height: 5px;
  315. width: 5px"/>
  316. <div class="about">
  317. <div class="name">{{state.full}}</div>
  318. <div class="status">
  319. <i class="fa fa-circle online"></i> online
  320. </div>
  321. </div>
  322. </li>
  323. {{/if}}
  324. </script>
  325. </body>
  326.  
  327. I want a private chat between the two users of the same channel and
  328. multiple users also there and profile pic with gravatar also want`enter
  329. code here.
  330.  
  331. // one non-admin user running ChatEngine
  332. let secretChat = new ChatEngine.Chat('unique-secret-channel');
  333. secretChat.invite(adminUserObject);
  334.  
  335. // This code goes in the admin client
  336. me.direct.on('$.invite', (payload) => {
  337. let privChat = new ChatEngine.Chat(payload.data.channel, true);
  338. });
  339.  
  340. // include the gravatar plugin script above
  341. ChatEngine.connect(uuid, { email: email@email.com });
  342.  
  343. // ...
  344.  
  345. for (let user of ChatEngine.users) {
  346. user.plugin(ChatEngine.plugin.gravatar());
  347. let div = document.createElement("div");
  348. div.innerHTML = '<img src="' + user.state().gravatar + '" height="40" width="40"/>';
  349. // add html to the web page
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement