I'm new to node.js but I know somewhat about socketstream web framework by using this I can easily call a server side node.js method from JavaScript. I don't know how to do this without using that framework. How can I call the node.js method from JavaScript?
我的新节点。但是我对socketstream web框架略知一二,通过使用这个我可以很容易地调用服务器端节点。js方法从JavaScript。如果不使用这个框架,我不知道该怎么做。如何调用节点。从JavaScript js方法?
The below code is using socketstream to call server side method. So I want to call the same server side method without using this framework.
下面的代码使用socketstream调用服务器端方法。所以我想不使用这个框架调用相同的服务器端方法。
ss.rpc('FileName.methodName',function(res){
alert(res);
});
1 个解决方案
#1
11
I'd suggest use Socket.IO
我建议使用socket . io
Server-side code
服务器端代码
var io = require('socket.io').listen(80); // initiate socket.io server
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' }); // Send data to client
// wait for the event raised by the client
socket.on('my other event', function (data) {
console.log(data);
});
});
and client-side
和客户端
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost'); // connec to server
socket.on('news', function (data) { // listen to news event raised by the server
console.log(data);
socket.emit('my other event', { my: 'data' }); // raise an event on the server
});
</script>
Alternatively, you can use a router function which calls some function on specific request from the client
或者,您可以使用路由器函数,该函数根据客户端的特定请求调用某些函数
var server = connect()
.use(function (req, res, next) {
var query;
var url_parts = url.parse(req.url, true);
query = url_parts.query;
if (req.method == 'GET') {
switch (url_parts.pathname) {
case '/somepath':
// do something
call_some_fn()
res.end();
break;
}
}
})
.listen(8080);
And fire AJAX
request using JQuery
并使用JQuery触发AJAX请求
$.ajax({
type: 'get',
url: '/somepath',
success: function (data) {
// use data
}
})
#1
11
I'd suggest use Socket.IO
我建议使用socket . io
Server-side code
服务器端代码
var io = require('socket.io').listen(80); // initiate socket.io server
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' }); // Send data to client
// wait for the event raised by the client
socket.on('my other event', function (data) {
console.log(data);
});
});
and client-side
和客户端
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost'); // connec to server
socket.on('news', function (data) { // listen to news event raised by the server
console.log(data);
socket.emit('my other event', { my: 'data' }); // raise an event on the server
});
</script>
Alternatively, you can use a router function which calls some function on specific request from the client
或者,您可以使用路由器函数,该函数根据客户端的特定请求调用某些函数
var server = connect()
.use(function (req, res, next) {
var query;
var url_parts = url.parse(req.url, true);
query = url_parts.query;
if (req.method == 'GET') {
switch (url_parts.pathname) {
case '/somepath':
// do something
call_some_fn()
res.end();
break;
}
}
})
.listen(8080);
And fire AJAX
request using JQuery
并使用JQuery触发AJAX请求
$.ajax({
type: 'get',
url: '/somepath',
success: function (data) {
// use data
}
})