Skip to content

Instantly share code, notes, and snippets.

@ww24
Created January 2, 2014 15:59
Show Gist options
  • Save ww24/8221338 to your computer and use it in GitHub Desktop.
Save ww24/8221338 to your computer and use it in GitHub Desktop.
WebSocket と Server-Sent Events を試す。
<!DOCTYPE html>
<html>
<head>
<title>WebSocket</title>
</head>
<body>
<h1>Hello</h1>
<script>
var event_src = new EventSource("http://localhost:3000/");
event_src.onopen = function (e) {
console.info("open");
};
event_src.onerror = function (err) {
console.error(err);
};
event_src.onmessage = function (e) {
console.log(e.data);
if (e.data === "sse.html") {
location.reload(true);
}
};
</script>
</body>
</html>
/**
* Server-Sent Events のテスト
*
*/
var http = require("http"),
events = require("events"),
path = require("path"),
fs = require("fs");
var notify = new events.EventEmitter();
// Create Server-Sent Events Server
http.createServer(function (req, res) {
// Set Header for Server-Sent Events
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
"Access-Control-Allow-Origin": "*"
});
res.write("\n");
console.log("open");
// add msg event
notify.on("msg", msg);
function msg(data) {
res.write("data:" + data + "\n\n");
console.log(data);
}
// remove msg event
req.on("close", function () {
notify.removeListener("msg", msg);
console.log("closed");
console.log("remoteAddress: " + req.connection.remoteAddress);
});
}).listen(3000);
fs.watch(path.resolve(__dirname, "sse.html"), function (event) {
if (event === "change")
notify.emit("msg", "sse.html");
});
<!DOCTYPE html>
<html>
<head>
<title>WebSocket</title>
</head>
<body>
<h1>Hello</h1>
<script>
var ws = new WebSocket("ws://localhost:3300/");
ws.onopen = function () {
ws.send("ping");
console.info("open");
};
ws.onerror = function (err) {
console.error(err);
};
ws.onmessage = function (e) {
console.log(e.data);
if (e.data === "websocket.html") {
location.reload(true);
}
};
</script>
</body>
</html>
/**
* WebSocket のテスト
*
*/
var http = require("http"),
path = require("path"),
fs = require("fs"),
WebSocketServer = require("websocket").server;
var server = http.createServer(function (req, res) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end("websocket server");
}).listen(3300);
var wsServer = new WebSocketServer({
httpServer: server
});
wsServer.on("request", function (req) {
var ws = req.accept(null, req.origin);
ws.on("message", function (message) {
if (message.type === "utf8") {
console.log(message.utf8Data);
ws.sendUTF(message.utf8Data);
}
});
ws.on("close", function (reasonCode, description) {
console.log("closed");
console.log("remoteAddress: " + ws.remoteAddress);
console.log("reasonCode: " + reasonCode);
console.log("description: " + description);
});
});
fs.watch(path.resolve(__dirname, "websocket.html"), function (event) {
if (event === "change")
wsServer.broadcastUTF("websocket.html");
});
@ww24
Copy link
Author

ww24 commented Jan 2, 2014

websocket.js の実行前に npm install websocket を実行する必要あり。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment