Skip to content

Commit

Permalink
Basic Socket Server
Browse files Browse the repository at this point in the history
  • Loading branch information
Asher978 committed Sep 5, 2018
0 parents commit 56b1a76
Show file tree
Hide file tree
Showing 12 changed files with 9,935 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"presets": [
[
"env",
{
"targets": {
"node": "current",
"browsers": "last 2 versions"
}
}
],
"stage-0",
"es2015",
"es2016",
"es2017",
"react"
],
"plugins": [
[
"babel-root-import",
{
"rootPathSuffix": "backend"
}
],
"transform-class-properties"
]
}
9 changes: 9 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "airbnb",
"rules": {},
"settings": {
"import/resolver": {
"babel-plugin-root-import": {}
}
}
}
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Node.
npm-debug.log
coverage/
node_modules/

# env
.env

# Public (distribution).
public/*bundle.js

# Config files.
config/*
!config/default.js
!config/test.js
Empty file added README.md
Empty file.
55 changes: 55 additions & 0 deletions client/components/AppRoot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React, { Component } from "react";
import io from "socket.io-client";

// random color helper
function selectColor(colorNum, colors) {
if (colors < 1) colors = 1;
return "hsl(" + ((colorNum * (360 / colors)) % 360) + ",100%,50%)";
}

export default class AppRoot extends Component {
state = {
client: null,
myData: []
};
componentDidMount() {
this.socket = io.connect("http://localhost:3334");
this.socket.on("connect", () => {
this.setState({ client: this.socket });
});

this.socket.on("TIMEREVENT", data => {
this.setState(prevState => ({
myData: [...prevState.myData, data]
}));
});
}

renderSocketServerData = () => {
const { myData } = this.state;
if (myData.length) {
return myData.map((item, i) => {
return (
<li
key={i}
style={{
color: `${selectColor(12, item)}`
}}
>
Data received from socketserver ---> {item}
</li>
);
});
}
};
render() {
return (
<div>
<h1>Socket.Io</h1>
<h2>My Socket Id is:</h2>
<p>{this.state.client ? this.state.client.id : "Not Connected Yet"}</p>
<ul>{this.renderSocketServerData()}</ul>
</div>
);
}
}
9 changes: 9 additions & 0 deletions client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";
import { render } from "react-dom";
import AppRoot from "./components/AppRoot";

function main() {
render(<AppRoot />, document.querySelector("#root"));
}

document.addEventListener("DOMContentLoaded", main);
Loading

0 comments on commit 56b1a76

Please sign in to comment.