Commit 747499a5 authored by Mahmoud Ibrahim Mohammad's avatar Mahmoud Ibrahim Mohammad
Browse files

update server and client

parent 2c773109
Pipeline #81664 failed with stages
in 28 seconds
<!DOCTYPE HTML>
<html>
<head>
<script src="./main.js" type="text/javascript"></script>
<script src="index.js"></script>
</head>
<body>
<div id="loading">
Laster....
</div>
<ul id="tavle" ></ul>
<div>
<p>Navn: <input type="text" id="name"></p>
<p>Tekst: <input type="text" id="message" > <button id="submit" onclick="chat.sendMelding()">Send</button></p>
</div>
</body>
</html>
\ No newline at end of file
class Chat {
constructor() {
var url = window.location.hostname;
this.ws = new WebSocket("ws://" + url + ":3001");
this.ws.onerror = (error) => {
$("#loading").html("ikke tilkoblet");
};
this.ws.onopen = () => {
$("#loading").html("WebSocket");
};
this.ws.onmessage = (event) => {
var json=JSON.parse(event.data);
if(json.message){
var date = new Date();
var hour = date.getHours();
var minute = date.getMinutes();
var time = hour + ':' + minute;
$("#tavle").append('<li>' + time + ' <b>' + json.message.name + ':</b> ' + json.message.text +'</li>');
}
};
this.ws.onclose = function(message) {
$("#loading").html("ikke tilkoblet: Forbindelse lukket");
};
}
sendMelding() {
const navn = document.getElementById("name").value;
const tekst = document.getElementById("message").value;
if(this.ws.readyState==1) {
var json={"message": { "name": navn, "text": tekst}};
this.ws.send(JSON.stringify(json));
}
}
}
var chat;
$(document).ready(function(){
chat = new Chat();
});
\ No newline at end of file
This diff is collapsed.
{
"name": "app",
"version": "1.0.0",
"scripts": {
"start": "babel-node src/server.js",
"test": "jest --detectOpenHandles --forceExit"
},
"jest": {
"testRegex": "./tests/.+Test\\.js$",
"testEnvironment": "node"
},
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/node": "^7.8.7",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/preset-env": "^7.9.0",
"@babel/preset-flow": "^7.9.0",
"axios": "^0.19.2",
"babel-jest": "^25.1.0",
"jest": "^25.1.0"
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
This is App
</body>
</html>
{
"name": "websocket_server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Mahmoud Ibrahim",
"license": "",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.2",
"ws": "^7.2.3"
}
}
'use strict';
var express = require('express');
var WebSocket = require('ws');
var http_server = express();
// To download and start WS client
http_server.use(express.static(__dirname + "/../client"));
var ws_server = new WebSocket.Server({ port: 3001 });
var forbindelser = [];
var meldinger = [];
ws_server.on('connection', (connection) => {
console.log('forbindelse er satt opp');
forbindelser.push(connection);
meldinger.forEach((message) => {
connection.send(message);
});
connection.on('message', (message) => {
console.log("melding fra client: "+message);
meldinger.push(message);
ws_server.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
});
connection.on('close', () => {
console.log("Forbindelse lukket");
});
connection.on('error', (error) => {
console.error("Error: "+error.message);
});
});
var server = http_server.listen(3000, () => {
console.log('Server started on port 3000,and'+ '\nWS server is listening on 3001 ');
});
\ No newline at end of file
// @flow
import express from 'express';
import path from 'path';
let app = express();
app.use(express.static(path.join(__dirname, '../public')));
// The listen promise can be used to wait for the web server to start (for instance in your tests)
export let appListen = new Promise<void>((resolve, reject) => {
app.listen(3000, (error: ?Error) => {
if (error) return reject(error.message);
console.log('Express server started');
resolve();
});
});
// @flow
import axios from 'axios';
import type { $AxiosError } from 'axios';
import { appListen } from '../src/server';
axios.defaults.baseURL = 'http://localhost:3000';
beforeAll(() => appListen);
describe('App responds on requests', () => {
it('GET / returns 200', done => {
axios.get('/').then(response => {
expect(response.status).toEqual(200);
done();
});
});
it('PUT / returns 404', done => {
axios.put('/').catch((error: $AxiosError<mixed>) => {
if (!error.response) throw new Error();
expect(error.response.status).toEqual(404);
done();
});
});
});
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
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