A story about making a chat program in 10 minutes with WebSocket with VBScript on mac and talking with audience

I made the Chat program in 10 minutes at that day.

I used a programming language “OpenVBS” that compatible with VBScript.

$ ./oscript sample3.obs

This chat program is a web application, you can start http server by this command.

<!DOCTYPE html>
<html>
<head>
  <title>Chat</title>
  <script>
    let sock = new WebSocket("ws://localhost:8000/chat.osk");
    sock.onmessage = function(e){
      let elm = document.createElement("li");
      elm.innerText = e.data;
      document.getElementById("output").insertBefore(elm, document.getElementById("output").firstChild);
    };
</script>
</head>

<body>
  <input type="text" onkeypress="if(event.keyCode==13){ sock.send(this.value); this.value=''; }">
  <ul id="output"></ul>
</body>
</html>

Let’s make such a file “chat.html” in “wwwroot” directory, this is a simple thing that send string to server and display strings when received from server.

last = 0
client.send("Welcome!")

function on_data(data)
    if len(data) then
        application(application.contents.count) = data
    end if

    do while last < application.contents.count
        client.send(application(last++))
    loop
end function

The WebSocket service is this “chat.osk”, this is a simple thing that store string to memory from client and send back new chats to client.

It will work on http://localhost:8000/chat.html ! I used ngrok to open to the global this local chat and enjoy conversation with audience.

This sample chat is included in OpenVBS ver.20191209, please use this for you live coding 😉 . (NOTE: Multithread not supported yet…)










.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s