How to use socket.io and jQuery to load a file - javascript

Using socket.io and jQuery how do I send a file (binary array) to my server? This is what I've tried:
<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
var socket = io.connect('http://' + document.domain + ':' + location.port);
$('form#upload_form').submit(function (event) {
//var File fileObj = new File($('#ssa_data').val());
socket.emit('certificate_upload', {transport_key: $('#small_file').val()});
return false;
});
});
</script>

Related

jQuery - d3.v3.min.js error 404 while building Spring Boot and Neo4j app

I'm trying to make application with Spring Boot, Neo4j and jQuery. For that I used Neo4j example project from github, but I'm getting error in those lines:
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="https://d3js.org/d3.v3.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function () {
function showMovie(title) {
$.get("/movies/search/findByTitle?title=" + encodeURIComponent(title), // todo fix paramter in SDN
function (data) {
if (!data) return;
var movie = data;
$("#title").text(movie.title);
$("#poster").attr("src","/posters/"+encodeURIComponent(movie.title)+".jpg");
var $list = $("#crew").empty();
movie.roles.forEach(function (cast) {
$.get(cast._links.person.href, function(personData) {
var person = personData.name;
var job = cast.job || "acted";
$list.append($("<li>" + person + " " +job + (job == "acted"?" as " + cast.roles.join(", ") : "") + "</li>"));
});
});
}, "json");
return false;
}
</script>
I'm getting error 404 when trying to use two first scripts. I'm running this project on my local server.
EDIT.
Here is response with an error:

get myTasks from mySite

Hello i try to get myTasks for the current User from sharepoint 2013. I tried it with the Rest-API and with JSON but i always get an error. Acording to the debugger ExecuteOrDelayUntilScriptLoaded is undefined.
Here is my little Javascript i got so far:
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="/_layouts/15/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.0/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/knockout/knockout-2.1.0.js"></script>
<script type="text/javascript" src="/sps10/EDV/jquery.js"></script>
<script type="text/javascript">
var context, userSessionManager ,userSession , query , myTasks ;
$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(function () {
context = SP.ClientContext.get_current();
ExecuteOrDelayUntilScriptLoaded(function () {
userSessionManager = new SP.WorkManagement.OM.UserOrderedSessionManager(context);
userSession = userSessionManager.createSession();
query = new SP.WorkManagement.OM.TaskQuery(context);
myTasks = userSession.readTasks(query);
}, "sp.workmanagement.js");
getMyTasks();
}, "sp.js");
});
function getMyTasks() {
context.load(myTasks);
context.executeQueryAsync(onGetMyTasksSuccess, onGetMyTasksFail);
}
// This function is executed if the above call is successful
function onGetMyTasksSuccess() {
console.log("Successfully retrieved tasks...");
var taskEnumerator = myTasks.getEnumerator();
while (taskEnumerator.moveNext()) {
var task = taskEnumerator.current;
console.log("Task: " + task.get_id() + " - " + task.get_name());
}
}
// This function is executed if the above call fails
function onGetMyTasksFail(sender, args) {
console.log('Failed to get tasks. Error:' + args.get_message());
}
</script>
So any help or advise would be great. If you want i can post my Code with the rest-api too but the error stays the same.
Thanks for your help and time.
Here is now the way to query the tasks from my site in a html file with javascript. The most important thing is to load the src file in the right order and to use only the jquery-min file not the normal jQuery file.
<script src="/_layouts/15/init.js" type="text/javascript"></script>
<script src="/_layouts/15/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/_layouts/15/sp.core.js" type="text/javascript"></script>
<script src="/_layouts/15/sp.runtime.js" type="text/javascript"></script>
<script src="/_layouts/15/sp.js" type="text/javascript"></script>
<script src="/_layouts/15/sp.workmanagement.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
'use strict';
var context = new SP.ClientContext.get_current();
var userSessionManager = new SP.WorkManagement.OM.UserOrderedSessionManager(context);
var userSession = userSessionManager.createSession();
var query = new SP.WorkManagement.OM.TaskQuery(context);
var myTasks = userSession.readTasks(query);
var tasks = [];
getMyTasks();
function getMyTasks() {
context.load(myTasks);
context.executeQueryAsync(onGetMyTasksSuccess, onGetMyTasksFail);
}
function onGetMyTasksSuccess() {
console.log("Successfully retrieved tasks...");
var taskEnumerator = myTasks.getEnumerator();
while (taskEnumerator.moveNext()) {
var task = taskEnumerator.current;
console.log("Task: " + task.get_id() + " - Taskname: " + task.get_name() + " - Beschreibung: " + task.get_description() + " - dueDatum: " + task.get_dueDate() + " - Startdatum: " + task.get_startDate() + " - Persönlich: " + task.get_isPersonal() + " - Fertiggestellt: " + task.get_isCompleted());
tasks.push({
taskName: task.get_name(),
description: task.get_description(),
dueDate: task.get_dueDate(),
startDate: task.get_startDate(),
personally: task.get_isPersonal(),
complete: task.get_isCompleted()
});
}
console.log(tasks);
}
// This function is executed if the above call fails
function onGetMyTasksFail(sender, args) {
console.log('Failed to get tasks. Error:' + args.get_message());
}
And here is a nice little blog post with all the possibilities:

MQTT for Web Application

I was trying to develop a simple web application using the MQTT Broker. I used Mosca as the broker on localhost. First I tried out a program copied from the web to see how MQTT works. This is the program.
home.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script src="mqttws31.js" type="text/javascript"></script>
<script src="client.js">
</script>
</head>
<body onload="init();">
</body>
</html>
client.js
var wsbroker = "127.0.0.1"; //mqtt websocket enabled broker
var wsport = 3000 // port for above
var client = new Paho.MQTT.Client(wsbroker, wsport,
"myclientid_" + parseInt(Math.random() * 100, 10));
client.onConnectionLost = function (responseObject) {
alert("connection lost: " + responseObject.errorMessage);
};
client.onMessageArrived = function (message) {
alert(message);//.destinationName, ' -- ', message.payloadString);
};
var options = {
timeout: 3,
onSuccess: function () {
alert("mqtt connected");
// Connection succeeded; subscribe to our topic, you can add multile lines of these
client.subscribe('temp/random', {qos: 1});
//use the below if you want to publish to a topic on connect
message = new Paho.MQTT.Message("Hello");
message.destinationName = "/World";
client.send(message);
},
onFailure: function (message) {
alert("Connection failed: " + message.errorMessage);
}
};
function init() {
client.connect(options);
}
This program worked when I tried to access home.html in te web browser. I could see the log being generated in Mosca's console too. However, as visible, this program wasn't a very neat example. For that reason I tried to make a few changes to make the code readable.
This is my code after I made the changes -
home.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script src="mqttws31.js" type="text/javascript"></script>
<script src="client.js">
</script>
</head>
<body onload="init();">
</body>
</html>
client.js
var wsbroker = "127.0.0.1";
var wsport = 3000
var client = new Paho.MQTT.Client(wsbroker, wsport,"myclientid_" + parseInt(Math.random() * 100, 10));
function onMessageArrived(message) {
document.write(message.payload);
};
function onSuccess() {
document.write("Connected");
client.subscribe('temp/random');
};
function onFailure(message) {
document.write("Connection Failed. Error : " + message.errorMessage);
};
function onConnectionLost(message) {
document.write("Connection Lost. Error : " + message.errorMessage);
};
var options = {
timeout: 3,
onSuccess: onSuccess,
onFailure = onFailure
};
function init() {
client.connect(options);
client.onMessageArrived = onMessageArrived,
client.onConnectionLost = onConnectionLost,
};
I have got a Python script running which publishes value. However, no output is being generated. I checked the Mosca console and noted that no new connections were made. I have just started learning Javascript. I am not sure if my new code is syntactically correct.
Couple changes will fix this.
First, you have onFailure = instead of onFailure:
Next, you want to set your client.onMessageArrived and client.onConnectionLost before you call connect, not after.
Those 2 changes result in
var wsbroker = "127.0.0.1";
var wsport = 3000
var client = new Paho.MQTT.Client(wsbroker, wsport,"myclientid_" + parseInt(Math.random() * 100, 10));
function onMessageArrived(message) {
document.write(message.payload);
};
function onSuccess() {
document.write("Connected");
client.subscribe('temp/random');
};
function onFailure(message) {
document.write("Connection Failed. Error : " + message.errorMessage);
};
function onConnectionLost(message) {
document.write("Connection Lost. Error : " + message.errorMessage);
};
var options = {
timeout: 3,
onSuccess: onSuccess,
onFailure: onFailure,
};
function init() {
console.log('connecting')
client.onMessageArrived = onMessageArrived,
client.onConnectionLost = onConnectionLost,
client.connect(options);
};

How can i connect multipule html pages using same sessionid in socket.io

I have the following client scripts
One is my is a function that sits on my index.html
<script src="/js/socket.io.js"></script>
<script type="text/javascript">
var socket = io.connect('https://private.c9.io/');
socket.emit('Start', 'tobi', function (data) {
var sessionid = socket.socket.sessionid;
console.log(data + " " + sessionid); // data will be 'sessionid'
});
</script>
and the other is a function that sits on my page2.html
<script src="/js/socket.io.js"></script>
<script type="text/javascript">
var socket = io.connect("https://private.c9.io/"); //i need to set session id from index.html
socket.emit('Start2', 'tobi', function (data) {
console.log(data); // data will be 'result'
});
</script>
Problem is when I use socket connect on each page the sessionid is diffrent and I need
the sessionid of socket.io to be the same for both.
I just need help on how I can set my session id on the page2.html function.
Can anyone please help?

Capture audio and upload it

I'm working on mobile device (iOS). I develop a hybrid application using HTML/CSS/Javascript.
I have this code founded on Apache Cordova API :
<!DOCTYPE html>
<html>
<head>
<title>Capture Audio</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
<script type="text/javascript" charset="utf-8" src="json2.js"></script>
<script type="text/javascript" charset="utf-8">
// Called when capture operation is finished
//
function captureSuccess(mediaFiles) {
var i, len;
for (i = 0, len = mediaFiles.length; i < len; i += 1) {
uploadFile(mediaFiles[i]);
}
}
// Called if something bad happens.
//
function captureError(error) {
var msg = 'An error occurred during capture: ' + error.code;
navigator.notification.alert(msg, null, 'Uh oh!');
}
// A button will call this function
//
function captureAudio() {
// Launch device audio recording application,
// allowing user to capture up to 2 audio clips
navigator.device.capture.captureAudio(captureSuccess, captureError, {limit: 2});
}
// Upload files to server
function uploadFile(mediaFile) {
var ft = new FileTransfer(),
path = mediaFile.fullPath,
name = mediaFile.name;
ft.upload(path,
"http://my.domain.com/upload.php",
function(result) {
console.log('Upload success: ' + result.responseCode);
console.log(result.bytesSent + ' bytes sent');
},
function(error) {
console.log('Error uploading file ' + path + ': ' + error.code);
},
{ fileName: name });
}
</script>
</head>
<body>
<button onclick="captureAudio();">Capture Audio</button> <br>
</body>
</html>
I can capture my voice with it and in theory upload it to
http://my.domain.com/upload.php
But I wonder how to adapt this code to upload file on my Dropbox . Is it possible to upload file to Dropbox as easy as that ?
As #sinaneker mentioned, you can do this server-side with PHP (or other languages).
But you can also do this directly from JavaScript using Dropbox's JavaScript library: https://github.com/dropbox/dropbox-js

Categories

Resources