Skip to content

Instantly share code, notes, and snippets.

const WebSocket = require("ws");
const express = require("express");
const app = express()
const path = require("path")
app.use("/",express.static(path.resolve(__dirname, "../client")))
const myServer = app.listen(9876) // regular http server using node express which serves your webpage
const wsServer = new WebSocket.Server({
//Websocekt variables
const url = "ws://localhost:9876/myWebsocket"
const mywsServer = new WebSocket(url)
//DOM Elements
const myMessages = document.getElementById("messages")
const myInput = document.getElementById("message")
const sendBtn = document.getElementById("send")
sendBtn.disabled = true
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSockets</title>
</head>
<body>
<!-- Simple HTML page for sending messages with input box and send button-->
@RanjanSushant
RanjanSushant / weather_scraping_demo.py
Last active December 20, 2021 00:04
Codesphere web scraping with Python example
#------- Importing required libraries --------
import pandas as pd
import requests
from bs4 import BeautifulSoup
#------- URL of the WEBPAGE you want to SCRAPE --------------
url = "https://forecast.weather.gov/MapClick.php?lat=41.8843&lon=-87.6324#.YbH0ptBBxPY"
#------- Sending an HTTP request ----------
response = requests.get(url)
@RanjanSushant
RanjanSushant / bell_state.js
Created December 24, 2021 12:44
codesphere_q_js_demo
//creating the circuit for the bell state
const phiPlus = Q`
H-X#0
I-X#1
`
//rendering the circuit on the page
document.body.appendChild(phiPlus.toDom())
@RanjanSushant
RanjanSushant / primitves.ts
Created January 4, 2022 07:43
Primitives in TypeScript
let message: string = "Hello World"; // string type variable
let myNum: number = 42; // number type variable
let isReal: boolean = true; // boolean type variable
let special: any = "Any type"; // any type variable
special = 13; // any type doesn't cause any type checking errors
@RanjanSushant
RanjanSushant / index.html
Created January 4, 2022 08:03
TypeScript demo HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TypeScript Demo</title>
</head>
<body>
<h1>Intro to TypeScript</h1>
@RanjanSushant
RanjanSushant / app.ts
Created January 4, 2022 08:09
TypeScript Demo code
// DOM Elements
const addBtn = document.getElementById("add-btn");
const number1 = document.getElementById("num1")! as HTMLInputElement;
const number2 = document.getElementById("num2")! as HTMLInputElement;
// function to add two numbers and return the result
function addNum(num1:number, num2:number): number {
return num1 + num2;
}
@RanjanSushant
RanjanSushant / twilio_app.js
Created January 10, 2022 18:13
Codesphere Twilio NodeJs text demo app
// Getting the environment variables from .env file. This needs to be at the top
require('dotenv').config();
// Saving the account sid and auth token values from environment variable to local variables
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
//Twilio client creation
const client = require('twilio')(accountSid, authToken);
@RanjanSushant
RanjanSushant / demo.js
Created January 29, 2022 07:54
Codesphere Jest demo javascript file and testing file
//case sensitive palindrome check for strings
function isPalindrome(str) {
const reversedString = reverseStr(str);
return reversedString === str;
}
//function to reverse string
function reverseStr(str) {
str = str.split("").reverse().join("");
return str;