Skip to content

socket.io v3 & v4 #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set -e

# setup server
cd socket.io.server
cd socket.io.server/v2
npm i
./node_modules/.bin/pm2 start index.js

Expand All @@ -11,4 +11,4 @@ cd ../test_integration
flutter drive

# Kill server
./../socket.io.server/node_modules/.bin/pm2 kill
./../socket.io.server/v2/node_modules/.bin/pm2 kill
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: adhara_socket_io
description: Websocket by socket.io for flutter by adhara, supports both iOS and Android
version: 1.0.0
author: Rohit R. Abbadi <rohit@infitio.com>
homepage: https://github.com/infitio/flutter_socket_io

environment:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
43 changes: 43 additions & 0 deletions socket.io.server/v3/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head><title>Adhara Socket IO integration test example</title></head>
<body>
<div id="console"></div>
<script src="/socket.io/socket.io.js"></script>
<script>
let cd = document.getElementById("console");
function ptoc(){
let a = Array.prototype.slice.apply(arguments);
a = a.map(_=>{
if(_ instanceof Object) return JSON.stringify(_);
return _;
})
cd.innerHTML += `<br />${a.join(', ')}`;
console.log(arguments);
}
ptoc("connecting...");
const socket = io({transports:["websocket"]});
socket.on("connect", (data)=>{
ptoc("connected", data);
});
var disconnected = false;
socket.on("disconnect", (data)=>{
ptoc("disconnected", data);
});
socket.emit("ack-message", "hey yo!", function(ackmsg){
ptoc(`ACK::${ackmsg}`);
});
socket.on('counter', ptoc);
var intervalId = setInterval(() => {
if (disconnected) {
stopInterval(intervalId);
} else {
socket.emit('next');
}
}, 500);
setTimeout(() => {
socket.disconnect();
}, 10000);
</script>
</body>
</html>
99 changes: 99 additions & 0 deletions socket.io.server/v3/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
var fs = require("fs");

function handler(req, res) {
fs.readFile(
__dirname + (req.url == "/" ? "/index.html" : req.url),
function (err, data) {
if (err) {
res.writeHead(500);
return res.end("Error loading index.html");
}
res.writeHead(200);
res.end(data);
}
);
}

var app = require("http").createServer(handler);
var io = require("socket.io")(app);
console.log("listening on 7070...");
app.listen(7070, "0.0.0.0");

var socketId = 0;
var counters = {};
var _sockets = new Set();

function listenToASocket(socket, namespace) {
var _currentSocketId = ++socketId;
_sockets.add(socket);

console.log(
`\x1b[32m✓✓✓\x1b[0m new connection with socketId ${_currentSocketId}
namespace: ${namespace ? namespace : "-NA-"}
timestamp: ${socket.handshake.query.timestamp}
transport: ${socket.conn.transport.name}
total active sockets: ${_sockets.size}
\x1b[43m\x1b[30m ${new Date()} \x1b[0m`
);

socket.emit("namespace", !!namespace);
socket.emit("type:string", "String message back to client");
socket.emit("type:bool", true);
socket.emit("type:number", 123);
socket.emit("type:object", { hello: "world" });
socket.emit("type:list", ["hello", 123, { key: "value" }]);
socket.on("data", function () {
let args = Array.prototype.slice.call(arguments);
console.log(
`data event received with ${args.length} args: ${args.map(
(arg) => arg + " is " + typeof arg
)}`
);
});
socket.on("echo", function () {
//`arguments` can be extracted only if this is an anonymous function and not an arrow => syntax
console.log(">>>arguments ", arguments);
let args = Array.prototype.slice.call(arguments);
console.log(`echo event received with ${args.length} args: ${args}`);
args.unshift("echo");
socket.emit.apply(socket, args);
});
socket.on("next", function () {
if (!counters[_currentSocketId]) {
counters[_currentSocketId] = 0;
}
counters[_currentSocketId] += 1;
socket.emit("counter", counters[_currentSocketId]);
});
socket.on("ack-message", function () {
let args = Array.prototype.slice.call(arguments);
fn = args.pop();
console.log(
`received ack message with args length: ${
args.length
} and content: ${JSON.stringify(args)}. Sending back ack!`
);
fn.apply(fn, args);
});
socket.on("disconnect", () => {
_sockets.delete(socket);
console.log(
`\x1b[31m✘✘✘\x1b[0m Socket disconnected with socketId ${_currentSocketId}
transport: ${socket.conn.transport.name},
total active sockets: ${_sockets.size}
\x1b[43m\x1b[30m ${new Date()} \x1b[0m`
);
});
}

io.on("connection", function (socket) {
listenToASocket(socket, null);
});

io.of("/adhara").on("connection", function (socket) {
listenToASocket(socket, "/adhara");
});

process.on("SIGINT", function () {
process.exit();
});
36 changes: 36 additions & 0 deletions socket.io.server/v3/index2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<title>Socket IO example</title>
</head>
<body>
<div id="console"></div>
<script src="/socket.io/socket.io.js"></script>
<script>
let cd = document.getElementById("console");
function ptoc(){
let a = Array.prototype.slice.apply(arguments);
a = a.map(_=>{
if(_ instanceof Object) return JSON.stringify(_);
return _;
})
cd.innerHTML += `<br />${a.join(', ')}`;
console.log(arguments);
}
ptoc("connecting...");
const socket = io({transports:["websocket"]});
socket.on("connect", (data)=>{
ptoc("connected", data);
});
socket.on("disconnect", (data)=>{
ptoc("disconnected", data);
});
socket.on("news", (data)=>{
ptoc("news...", data);
});
socket.emit("ack-message", "hey yo!", function(ackmsg){
console.log("ACK::"+ackmsg);
});
</script>
</body>
</html>
Loading