Browse Source

basics of like the underlying chat system

Getty Ritter 4 years ago
commit
ac05f084af
7 changed files with 166 additions and 0 deletions
  1. 5 0
      .gitignore
  2. 38 0
      parley.py
  3. 9 0
      schema.sql
  4. 30 0
      static/index.html
  5. 2 0
      static/jquery-3.4.1.min.js
  6. 51 0
      static/main.css
  7. 31 0
      static/main.js

+ 5 - 0
.gitignore

@@ -0,0 +1,5 @@
+bin
+include
+lib
+*~
+.mypy_cache

+ 38 - 0
parley.py

@@ -0,0 +1,38 @@
+import asyncio
+import json
+import pysqlite3 as sql
+import sanic
+
+app = sanic.Sanic()
+
+@app.route("/static/<f>")
+async def static(request, f):
+    return await sanic.response.file('static/{}'.format(f))
+
+@app.route("/")
+async def index(request):
+    return await sanic.response.file('static/index.html')
+
+@app.websocket("/socket")
+async def socket(request, ws):
+    initial = await ws.recv()
+    config = json.loads(initial)
+    sanic.log.logger.info('connected websocket for {} in game {}'.format(
+        config['user'],
+        config['game']))
+
+    for x in range(50):
+        await ws.send(json.dumps({"author": "server", "content": str(x)}))
+        await asyncio.sleep(1)
+    # await ws.send(json.dumps({"author": "server", "content": "two"}))
+    # await asyncio.sleep(1)
+    # await ws.send(json.dumps({"author": "server", "content": "three"}))
+    # while True:
+    #     data = 'hello!'
+    #     print('Sending: ' + data)
+    #     await ws.send(data)
+    #     data = await ws.recv()
+    #     print('Received: ' + data)
+
+if __name__ == '__main__':
+    app.run()

+ 9 - 0
schema.sql

@@ -0,0 +1,9 @@
+CREATE TABLE games
+  ( id INTEGER PRIMARY KEY
+  , name TEXT NOT NULL
+  );
+
+CREATE TABLE chat
+  ( id INTEGER PRIMARY KEY
+  , 
+  );

+ 30 - 0
static/index.html

@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <title>Parley</title>
+    <link rel="stylesheet" type="text/css" href="/static/main.css" />
+    <script type="text/javascript" src="/static/jquery-3.4.1.min.js"></script>
+    <script type="text/javascript" src="/static/main.js"></script>
+  </head>
+  <body>
+    <div class="page">
+      <div class="header">
+        header
+      </div>
+      <div class="contents">
+        contents
+      </div>
+      <div class="chat">
+        <div class="msg">
+          <span class="author">gdritter:</span> hey what is up
+        </div>
+        <div class="msg">
+          <span class="author">gdritter:</span> nm u
+        </div>
+      </div>
+      <div class="footer">
+        footer
+      </div>
+    </div>
+  </body>
+</html>

File diff suppressed because it is too large
+ 2 - 0
static/jquery-3.4.1.min.js


+ 51 - 0
static/main.css

@@ -0,0 +1,51 @@
+* { box-sizing: border-box; }
+
+body {
+    font-family: "Fira Sans";
+}
+
+.page {
+    position: absolute;
+    top: 0;
+    left: 0;
+    display: grid;
+    grid-template-columns: 25% 25% 25% 25%;
+    grid-template-rows: 50px auto 50px;
+    width: 100%;
+    height: 100%;
+}
+
+.header {
+    border: 1px solid black;
+    grid-column: 1 / 5;
+    grid-row: 1;
+}
+
+.footer {
+    border: 1px solid black;
+    grid-column: 1 / 5;
+    grid-row: 3;
+}
+
+.contents {
+    border: 1px solid black;
+    grid-column: 1 / 4;
+    grid-row: 2;
+}
+
+.chat {
+    border: 1px solid black;
+    grid-column: 4 / 5;
+    grid-row: 2;
+    overflow: auto;
+    padding: 1em;
+}
+
+.author {
+    font-weight: bold;
+    color: #900;
+}
+
+.msg {
+    padding: 0.2em;
+}

+ 31 - 0
static/main.js

@@ -0,0 +1,31 @@
+window.onload = function() {
+    const config = {
+        "game": "testing",
+        "user": "test_user",
+    };
+
+    const rawHash = window.location.hash;
+    if (rawHash) {
+        const hash = rawHash.substr(1, rawHash.length).split(",");
+        for (let i in hash) {
+            let elements = hash[i].split(":");
+            if (elements.length != 2) {
+                continue;
+            }
+            config[elements[0]] = elements[1];
+        }
+    }
+
+    const socket = new WebSocket("ws://localhost:8000/socket");
+    socket.addEventListener("open", function (event) {
+        console.log("Connected to server!");
+        socket.send(JSON.stringify(config));
+    });
+
+    socket.addEventListener("message", function (event) {
+        msg = JSON.parse(event.data);
+        var chat = $(".chat");
+        chat.append(`<div class="msg"><span class="author">${msg.author}:</span> ${msg.content}</div>`);
+        chat.animate({scrollTop: chat.prop('scrollHeight')});
+    });
+};