![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Redux / React binding for Socket.io
npm install redux.io --save
/store.js
import { createStore, combineReducers } from 'redux';
import { reducer as socket } from 'redux.io';
/* Get your reference to Socket.io. */
/* Whether you're bundling with the client: */
import io from 'socket.io-client';
/* Or if you're having the file served: */
const io = window.io;
const rootReducer = combineReducers({
socket: socket(io),
});
const store = createStore(rootReducer);
/app.js
import React from 'react';
import { Provider } from 'react-redux';
import { render } from 'react-dom';
import { withSocket as io } from 'redux.io';
import store from './store';
// Playing in the dark.
class ZombieLand extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
players: 0,
infected: 0,
infectious: false,
x: 0,
y: 0,
};
this.keyUp = this.keyUp.bind(this);
}
componentDidMount() {
const { socket } = this.props;
socket
.on('count', players => this.setState({ players }))
.on('infected', infected => this.setState({ infected }))
.on('infectious', infectious => this.setState({ infectious }))
.connect();
document.body.addEventListener('keydown', this.keyUp);
}
componentDidUpdate(_props, _state) {
const { x, y } = this.state;
if (_state.x !== x || _state.y !== y) this.move();
}
componentWillUnmount() {
document.body.removeEventListener('keydown', this.keyUp);
}
move() {
const { socket } = this.props;
const { x, y } = this.state;
socket.emit('position', x, y);
}
keyUp(event) {
event.preventDefault();
const { keyCode } = event;
const { x, y } = this.state;
switch (keyCode) {
default:
break;
case 37: /* Arrow Left */
case 39: /* Arrow Right */
case 65: /* a */
case 68: /* d */ {
if ([39, 68].includes(keyCode)) {
if (x + 1 <= 100) this.setState({ x: x + 1 });
} else if ([37, 65].includes(keyCode)) {
if (x - 1 >= 0) this.setState({ x: x - 1 });
}
break;
}
case 38: /* Arrow Up */
case 40: /* Arrow Down */
case 87: /* w */
case 83: /* s */ {
if ([40, 83].includes(keyCode)) {
if (y + 1 <= 100) this.setState({ y: y + 1 });
} else if ([38, 87].includes(keyCode)) {
if (y - 1 >= 0) this.setState({ y: y - 1 });
}
break;
}
}
}
render() {
const { socket } = this.props;
const { x, y, players, infected, infectious } = this.state;
return socket.connected ? (
<section>
<header>
<h2>Welcome To Zombie Land</h2>
<p>Players: {players}</p>
</header>
<p>
X: <span>{x}</span>, Y: <span>{y}</span>
</p>
<code>[PLACE GAME VIEW HERE]</code>
<footer>
<p>Infected: {infected}</p>
<p>
Are You Infected?{' '}
{infectious ? 'EAT BRAIN...' : 'Naaa You Still Good! RUN!'}
</p>
<button type="button" onClick={() => socket.disconnect()}>
Get Me Out Of Here...!
</button>
</footer>
</section>
) : (
<button type="button" onClick={() => socket.connect()}>
Step Inside, But Tread Lightly..
</button>
);
}
}
const ManHunt = io('/hunt', {
autoConnect: false,
})(ZombieLand);
const Root = () => (
<Provider store={store}>
<ManHunt />
</Provider>
);
render(<Root />, document.getElementById('root'));
/server.js
const http = require('http');
const server = http.createServer();
server.listen(process.env.PORT, error => {
if (error === undefined) {
const io = require('socket.io')(server);
const hunt = io.of('/hunt');
const players = new Map();
const positions = new Map();
const infected = new Set();
hunt.on('connect', socket => {
players.set(socket.id, [0, 0]);
hunt.emit('count', players.size);
socket
.on('position', (x, y) => {
const coords = [x, y];
const infectious = infected.has(socket.id);
if (positions.has(coords)) {
const position = positions.get(coords);
if (infectious) {
const victims = (position || []).filter(
id => infected.has(id) === false,
);
victims.forEach(id => {
infected.add(id);
hunt.to(id).emit('infectious', true);
});
if (victims.length) hunt.emit('infected', infected.size);
} else {
const vermin = (position || []).filter(id => infected.has(id));
if (vermin.length) {
infected.add(socket.id);
socket.emit('infectious', true);
hunt.emit('infected', infected.size);
}
}
position.push(socket.id);
positions.set(coords, position);
} else positions.set(coords, [socket.id]);
const current = players.get(socket.id);
const position = positions.get(current);
positions.set(
current,
(position || []).filter(id => id !== socket.id),
);
players.set(socket.id, coords);
})
.on('disconnect', () => {
const current = players.get(socket.id);
const position = positions.get(current);
if (position) {
position.splice(position.indexOf(socket.id), 1);
positions.set(current, position);
}
infected.delete(socket.id);
players.delete(socket.id);
hunt.emit('count', players.size);
});
});
}
});
This is what comes out of the box:
import { reducer, withSocket } from 'redux.io';
// or
const { reducer, withSocket } = require('redux.io');
It's up to you on how socket.io is delivered to the client. The only requirement is to pass socket.io to the redux.io reducer as the first parameter.
The complete of options as well as the defaultOptions list can be found here.
url (String)
options (Object || Function (ownProps))
Component (React element) optional
wrapperConfig (object) optional
it's worthwhile to explore the socket.io library if you're new to it. check it out.
0.2.0 - Breaking API changes, HoC Implementation.
0.1.0 - Initial Implementation.
FAQs
redux/react binding for websockets
We found that redux.io demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.