node, javascript error using soket.io [overlapping]

Asked 2 years ago, Updated 2 years ago, 79 views

(Two answers) 4 years ago

There is an error in client.js, but I don't know where to fix it.
It seems to be an error with the symbol <, but I don't use it anywhere, so it's refreshing.
If you are familiar with javascript, please let me know

Error Contents

SyntaxError: expected expression, got'<'client.js:1

http://d.hatena.ne.jp/tomo-ono/20110530/1306740692
I created it by referring to the site, but I don't know how to solve it

Source code for the project created using the above URL

client.js

$(function(){
var socket = newio.Socket(null, {port:8080});
socket.connect();

function iosHandleOrientation(event){
    variableData=event.accelerationIncludingGravity;
    socket.send(orientData.x+"+orientData.y+"+orientData.z);    
}

window.addEventListener("devicemotion",iosHandleOrientation, true);

socket.on('message', function(obj){
    data=obj.split("";
    $('#data') .append("<tr><td>"+parseFloat(data[0]).toFixed(3)+"</td><td>"+parseFloat(data[1]).toFixed(3)+"<td>td>2Float;"

});
});

server.js

varfs=require('fs');
var http=require('http'),
    io= require ('socket.io'),

server = http.createServer(function(req,res){
  fs.readFile('./socket.html', 'utf-8', doRead);
  function doReard(err,data){
    res.writeHead(200, {'Content-Type':'text/html'});
  res.write(data);
    res.end();
}
});
server.listen (8080);

var socket =io.listen(server);
socket.on('connection', function(client){
    client.on('message', function(message){
        client.broadcast(message);
        console.log(message);
    });

    client.on('disconnect', function(){
            console.log('Connection closed.');
    });
});

socket.html

<!DOCTYPE html>
<html lang="ja">
<metacharset="utf-8">
<head>
     <script type="text/javascript" src="https://cdn.socket.io/socket.io- 1.4.5.js">/script>
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">/script>
     <script src="client.js"></script>
</head>
<body>
<table id="data">
<tr>th>x>/th>y>/th>th>z>/th>>
</table>
</body>
</html>

javascript ios html iphone socket

2022-09-29 22:14

1 Answers

This may be because server.js returns socket.html regardless of the path of the request.

The browser looks at the tag <script src="client.js">/script> and requests to the server, but I think the error occurred because it was returned by html.

Use Express for ease of routing.

Related Resources
Hosting Static Files (static Files) in Express


2022-09-29 22:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.