Verified Commit cafd4aed authored by Aral Balkan's avatar Aral Balkan
Browse files

Closes #48: Create Windows version of the WebSocket chat example

parent 6802208f
......@@ -467,32 +467,34 @@ module.exports = (request, response) => {
<h3>Create the chat server.</h3>
<h4 class='no-js'>Linux and macOS</h4>
<pre class='linux macos'><code class='language-mixed'><span class='language-bash'>echo "</span><span class='language-js'>
<pre class='linux macos'><code class='language-mixed'><span class='language-bash'>echo '</span><span class='language-js'>
module.exports = function (client, request) {
// Set the room based on the route’s URL.
client.room = this.setRoom(request)
// Create a message handler.
client.on('message', message => {
client.on("message", message => {
// Broadcast a received message to everyone in the room.
this.broadcast(client, message)
})
}</span>
<span class='hljs-string'>"</span><span class='language-bash'> > chat/.dynamic/.wss/chat.js</span></code></pre>
<span class='hljs-string'>'</span><span class='language-bash'> > chat/.dynamic/.wss/chat.js</span></code></pre>
<h4 class='no-js'>Windows</h4>
<pre class='windows'><code class='language-mixed'><span class='language-powershell'>echo "</span><span class='language-js'>
<pre class='windows'><code class='language-mixed'><span class='language-powershell'>echo '</span><span class='language-js'>
module.exports = function (client, request) {
// Set the room based on the route’s URL.
client.room = this.setRoom(request)
// Create a message handler.
client.on('message', message => {
client.on("message", message => {
// Broadcast a received message to everyone in the room.
this.broadcast(client, message)
})
}</span>
<span class='hljs-string'>"</span> <span class='language-powershell'>| Out-File -Encoding UTF8 chat/.dynamic/.wss/chat.js</span></code></pre>
<span class='hljs-string'>'</span> <span class='language-powershell'>| Out-File -Encoding UTF8 chat/.dynamic/.wss/chat.js</span></code></pre>
<aside>
<h3>Tip</h3>
<p>
......@@ -502,7 +504,10 @@ module.exports = (request, response) => {
</li>
<li>
<h3>Create the chat client.</h3>
<pre><code class='language-mixed'><span class='language-bash'>echo '</span><span class='language-html'>
<h4 class='no-js'>Linux and macOS</h4>
<pre class='linux macos'><code class='language-mixed'><span class='language-bash'>echo '</span><span class='language-html'>
&lt;!doctype html&gt;
&lt;html lang="en"&gt;
&lt;title&gt;Chat&lt;/title&gt;
......@@ -531,6 +536,38 @@ module.exports = (request, response) => {
})</span><span class='language-html'>
&lt;/script&gt;
</span><span class='hljs-string'>'</span><span class='language-bash'> > chat/index.html</span></code></pre>
<h4 class='no-js'>Windows</h4>
<pre class='windows'><code class='language-mixed'><span class='language-bash'>echo '</span><span class='language-html'>
&lt;!doctype html&gt;
&lt;html lang="en"&gt;
&lt;title&gt;Chat&lt;/title&gt;
&lt;h1&gt;Chat&lt;/h1&gt;
&lt;form name="messageForm"&gt;
&lt;input name="message" type="text"&gt;
&lt;button&gt;Send&lt;/button&gt;
&lt;/form&gt;
&lt;h2&gt;Messages&lt;/h2&gt;
&lt;ul id="messageList"&gt;&lt;/ul&gt;
&lt;script&gt;</span><span class='language-js'>
const socket = new WebSocket("wss://localhost/chat")
const showMessage = message =&gt; {
const messageList = document.querySelector("#messageList")
messageList.innerHTML += `&lt;li&gt;${message}&lt;/li&gt;`
}
socket.onmessage = message =&gt; showMessage(message.data)
document.messageForm.addEventListener("submit", event =&gt; {
const message = event.target.message.value
socket.send(message)
showMessage(`&lt;strong&gt;${message}&lt;/strong&gt;`)
event.preventDefault()
})</span><span class='language-html'>
&lt;/script&gt;
</span><span class='hljs-string'>'</span><span class='language-powershell'>| Out-File -Encoding UTF8 chat/index.html</span></code></pre>
<aside>
<h3>Tip</h3>
<p>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment