I'm thinking of doing Socket communication between my PC browser and the server.The environment is as follows:
Ubuntu 14.04
apache22.4.7
"When you open a page with a browser, it connects to the server and sends a message ""hello""."For some reason, when I checked the Node.js log, when I checked the browser log, there was no connected log.
Node.js (server side) msg.js
varfs=require("fs");
var http=require("http");
var server = http.createServer(function(req,res){
res.writeHead(200, {"Contents-Type": "text/html"});
var output =fs.readFileSync("./index.html", "utf-8");
res.end(output);
});
// Preparing Socketio
vario=require('socket.io')(server);
// Client Connection Processing
io.on('connection', function(socket){
variable=socket.id;
console.log("client connected!!!");
console.log(id)
// Client Disconnect Action
socket.on('disconnect', function(){
console.log("client disconnected!!")
});
// Receive from client (socket.on)
socket.on("from_client", function(obj){
io.emit("from_server", obj);
console.log(obj)
});
});
server.listen (3000);
index.html
on the PC browser (client) side<metacharset="UTF-8">
<html>
<head>
</head>
<body>
<script src="http://www.ubuntu.jp/node/socket.io/socket.io.js"></script>
<script>
var socket=io.connect('ws://www.ubuntu.jp', {path:'/node/'});
socket.on('connect', function(){
console.log('connected');
socket.emit('from_client','fromPC browser');
socket.on('from_server', function(msg){
console.log(msg);
});
});
</script>
</body>
</html>
We are reverse-proxying the ws protocol with apache. The apache configuration is as follows:
<VirtualHost*:80>
ServerAdmin webmaster@localhost
ServerName ubuntu.jp
ServerAlias www.ubuntu.jp
DocumentRoot/var/ubuntu
Options FollowSymlinks MultiViews ExecCGI
DirectoryIndex index.html index.php
Alias/node/var/ubuntu/node
ProxyPass/node/http://localhost:3000/
ProxyPass/node/ws://localhost:3000/
ProxyPassReverse/node/http://localhost:3000/
ProxyPassReverse/node/ws://localhost:3000/
ErrorLog${APACHE_LOG_DIR}/ubuntu_error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerge
LogLevel warn
CustomLog${APACHE_LOG_DIR}/ubuntu_access.log combined
ServerSignature Off
<Directory"/var/ubuntu/">
Require all graded
</Directory>
</VirtualHost>
in your browser
http://www.ubuntu.jp/node/
You have accessed .I checked with Firefox, but the console log shows
SyntaxError: expected expression, got '<' (socket.io.js:1:0)
There is an error from socket.io.js.
If you know the cause and how to deal with it, please let us know.
node.js socket.io
The current code is res.write("Hello World!!!");
, so whenever I access the server with a browser, the text "Hello World!!!" is returned, and the contents of "html on the PC browser (client) side" are not displayed in the browser.
var http=require("http");
var server = http.createServer(function(req,res){
res.write("Hello World!"");
res.end();
});
Therefore, I think you need to modify the beginning of msg.js to return the contents of "html on the PC browser (client) side" when you access the server with your browser (assuming that "html on the PC browser (client) side" is named index.html):
varfs=require("fs");
var http=require("http");
var server = http.createServer(function(req,res){
res.writeHead(200, {"Content-Type": "text/html"});
var output =fs.readFileSync("./index.html", "utf-8");
res.end(output);
});
Reference Articles
© 2024 OneMinuteCode. All rights reserved.