Skip to content

Instantly share code, notes, and snippets.

@itgoldman
Last active August 18, 2021 09:09
Show Gist options
  • Save itgoldman/5dbfc1d456ad3e65925869b925579fd5 to your computer and use it in GitHub Desktop.
Save itgoldman/5dbfc1d456ad3e65925869b925579fd5 to your computer and use it in GitHub Desktop.
rejax-io-test.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "rejax-io-test.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyMcuiLq1KDntThThVH8ApKf",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/itgoldman/5dbfc1d456ad3e65925869b925579fd5/rejax-io-test.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 133
},
"id": "Yw-3q-IYenWa",
"outputId": "f62b192f-647c-448b-88f7-10a6fe2fffc0"
},
"source": [
"\"\"\"run client first. make sure app keys and secret match the domain (origin) of client \"\"\"\n",
"import IPython\n",
"\n",
"display(IPython.display.HTML('''\n",
"<!DOCTYPE html>\n",
"<html lang=\"en\">\n",
"<head>\n",
"\t<meta charset=\"UTF-8\">\n",
"\t<title>rejax.io example - client</title>\n",
"<!-- Rejax Loader -->\n",
"<script>(function (w, d, s, v, p) {\n",
"w[v] = w[v] || { listen: function (a, b) { this.l[a] = b }, l: {} };\n",
"w[v].app_key = p; var f = d.getElementsByTagName(s)[0], j = d.createElement(s);\n",
"j.async = true; j.src = 'https://rejax.io:3001/api/client'; f.parentNode.insertBefore(j, f);\n",
"})(this, document, 'script', 'Rejax', 'KEY5xLtIl4z9i74a5TYQPz3uTfvSamjWHnp')</script>\n",
"<!-- End Rejax Loader -->\n",
"</head>\n",
"<body>\n",
"\t<h1>rejax.io example - client</h1>\n",
"\t<ul>\n",
"\t\t<li>open developers console</li>\n",
"\t\t<li>watch incoming messages</li>\n",
"\t</ul>\n",
" wait for it...\n",
"\t<pre id=\"incoming\"></pre>\n",
"\t<script>\n",
"\t\tRejax.listen('my-channel-name', function (text) {\n",
"\t\t\t// received text from server\n",
"\t\t\tconsole.log(text)\n",
"\t\t\tdocument.getElementById('incoming').innerText += text + '\\\\n'\n",
"\t\t})\n",
"\t</script>\n",
"</body>\n",
"</html>\n",
"'''))\n"
],
"execution_count": 26,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"\n",
"<!DOCTYPE html>\n",
"<html lang=\"en\">\n",
"<head>\n",
"\t<meta charset=\"UTF-8\">\n",
"\t<title>rejax.io example - client</title>\n",
"<!-- Rejax Loader -->\n",
"<script>(function (w, d, s, v, p) {\n",
"w[v] = w[v] || { listen: function (a, b) { this.l[a] = b }, l: {} };\n",
"w[v].app_key = p; var f = d.getElementsByTagName(s)[0], j = d.createElement(s);\n",
"j.async = true; j.src = 'https://rejax.io:3001/api/client'; f.parentNode.insertBefore(j, f);\n",
"})(this, document, 'script', 'Rejax', 'KEY5xLtIl4z9i74a5TYQPz3uTfvSamjWHnp')</script>\n",
"<!-- End Rejax Loader -->\n",
"</head>\n",
"<body>\n",
"\t<h1>rejax.io example - client</h1>\n",
"\t<ul>\n",
"\t\t<li>open developers console</li>\n",
"\t\t<li>watch incoming messages</li>\n",
"\t</ul>\n",
" wait for it...\n",
"\t<pre id=\"incoming\"></pre>\n",
"\t<script>\n",
"\t\tRejax.listen('my-channel-name', function (text) {\n",
"\t\t\t// received text from server\n",
"\t\t\tconsole.log(text)\n",
"\t\t\tdocument.getElementById('incoming').innerText += text + '\\n'\n",
"\t\t})\n",
"\t</script>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "F269yBa_X__R",
"outputId": "3af52160-5fda-4f2f-a9d7-ba9ccab7de59"
},
"source": [
"\"\"\"send messaage from server\"\"\"\n",
"\n",
"key = 'KEY5xLtIl4z9i74a5TYQPz3uTfvSamjWHnp'\n",
"secret = 'SECRETAC3szHwARobUaMIb1E4UFawCN2edAUP6'\n",
"url = \"https://rejax.io:3001/api/server\"\n",
"body = {\n",
" 'app_key' : key,\n",
" 'app_secret' : secret,\n",
" 'channel' : 'my-channel-name',\n",
" 'text' : 'hello from server'\n",
"}\n",
"\n",
"\n",
"import urllib.request\n",
"import json\n",
"import ssl\n",
"ssl._create_default_https_context = ssl._create_unverified_context\n",
"req = urllib.request.Request(url)\n",
"req.add_header('Content-Type', 'application/json; charset=utf-8')\n",
"jsondata = json.dumps(body)\n",
"jsondataasbytes = jsondata.encode('utf-8') # needs to be bytes\n",
"req.add_header('Content-Length', len(jsondataasbytes))\n",
"response = urllib.request.urlopen(req, jsondataasbytes)\n",
"print (response.status)\n"
],
"execution_count": 23,
"outputs": [
{
"output_type": "stream",
"text": [
"200\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment