Simplest websocket with nodejs example

Simplest websocket with nodejs example

2018/06/14 Programming 0

1. express –view=ejs myspp
2. var server = http.createServer(app);

var socketio = require('socket.io')
var io = socketio.listen(server);
var x = 0;
io.on("connection", function (socket) {
	socket.on('message', function (data) {
		socket.emit('echo', 'from server : ' + data);
	});
	setInterval(function () { socket.emit('echo', 'send to client = ' + (x++) + '<br>'); }, 1);
});

3.

	<script src="/socket.io/socket.io.js"></script>
	<script>
		var socket = io.connect('http://localhost:3000');
		socket.emit('message', 'hello');
		socket.on('echo', function (data) {
			document.write(data);
		});
	</script>

Source : myapp

read count : 4

Leave a Reply

Your email address will not be published. Required fields are marked *