web-socket.js and WebSocket - javascript

I am trying to do a web socket implementaton for browser. Firefox, Chrome works great, but when i try in IE, it creates the socket object but never calls the timer.
WebSocket = function(url, protocol, proxyHost, proxyPort, headers) {
var self = this;
self.__id = WebSocket.__nextId++;
WebSocket.__instances[self.__id] = self;
self.readyState = WebSocket.CONNECTING;
self.bufferedAmount = 0;
self.__events = {};
// Uses setTimeout() to make sure __createFlash() runs after the caller sets ws.onopen etc.
// Otherwise, when onopen fires immediately, onopen is called before it is set.
setTimeout(function() {
WebSocket.__addTask(function() {
WebSocket.__flash.create(
self.__id, url, protocol, proxyHost || null, proxyPort || 0, headers || null);
});
}, 0);
};
What could be the reason ?

What version of IE are you talking about ?
What websocket library are you using ?
Web sockets are not supported by older version of IE. I think you need to use long polling instead of websockets for those older browsers...
If your using this : https://github.com/gimite/web-socket-js
It's stated in the Documentation :
It should work on: Google Chrome 4 or
later (just uses native
implementation) Firefox 3.x, 4.x,
Internet Explorer 8, 9 + Flash Player
10 or later It may or may not work on
other browsers such as Safari, Opera
or IE 6. Patch for these browsers are
appreciated, but I will not work on
fixing issues specific to these
browsers by myself.

Related

Can't get Web Audio API to work with iOS 11 Safari

So iOS 11 Safari was supposed to add support for the Web Audio API, but it still doesn't seem to work with this javascript code:
//called on page load
get_user_media = get_user_media || navigator.webkitGetUserMedia;
get_user_media = get_user_media || navigator.mozGetUserMedia;
get_user_media.call(navigator, { "audio": true }, use_stream, function () { });
function use_stream(stream){
var audio_context = new AudioContext();
var microphone = audio_context.createMediaStreamSource(stream);
window.source = microphone; // Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=934512
var script_processor = audio_context.createScriptProcessor(1024, 1, 1);
script_processor.connect(audio_context.destination);
microphone.connect(script_processor);
//do more stuff which involves processing the data from user's microphone...
}
I copy pasted most of this code, so I only have a cursory understanding of it. I know that it's supposed to (and does, on other browsers) capture the user's microphone for further processing. I know that the code breaks on the var audio_context = new AudioContext(); line (as in, no code after that is run), but don't have any error messages cause I don't have a mac which is required to debug iOS Safari (apple die already >_<) Anyone know what's going on and/or how to fix it?
e: forgot to mention that I looked it up and apparently I need the keyword "webkit" before using Web Audio API in Safari, but making it var audio_context = new webkitAudioContext(); doesn't work either
#TomW was on the right track - basically the webkitAudioContext is suspended unless it's created in direct response to the user's tap (before you get the stream).
See my answer at https://stackoverflow.com/a/46534088/933879 for more details and a working example.
Nothing works on mobile save to home screen apps. I issued a bug report to Apple developer. Got a response that it was a duplicate ( which means they know..no clue if or when they will actually fix it).

Server Site Events (SSE) connection abort with firefox

I wrote a little Chat using SSE for loading new messages.
It is working fine with Chrome, Safari and Opera while Firefox closes the connection after a few retries.
I'am using retry: 2000 (2s). Sometimes firefox is doing up to 10 events, sometimes only 1 or 2. I know from my serverstats, that more then 80% of my users are using firefox, so I need this working on firefox.
I'm using the latest version of firefox and I added a console.log() after every EventListener I'm using ('message', 'open', 'close').
Does anyone have an idea what to do?
Thanks a lot
think I solved the problem:
First I opend the EventSource in a function :
function chat_read() {
if(typeof(EventSource) !== "undefined") {
var source;
source = new EventSource('?link=chat_stream');
}
Now I tried to initialize the variable 'source' global (outside the function) and it is working now.
var source;
function chat_read() {
if(typeof(EventSource) !== "undefined") {
source = new EventSource('?link=chat_stream');
}
Beside this I wrote a function which is looking for the SSE 'open' event still firering and if it is not, it will restart the SSE.

IndexedDB with iOS 8/ Safari

We have a testcase to test indexeddb with different browsers and OS.
It is just simple test:
open database, add some data, retrieve some data
That is it. It is working perfectly in Chrome (39), Firefox (new versions), MacBook Pro with OSX 9.5, Android based Browsers.
When we try with Ipad3 with iOS 8, the page is not doing anything. And we can not see any errors too.
Any ideas, how to fix the problem?
We used indexeddb.shim.js file that suppose to help, but still does not work.
if (!window.indexedDB) {
window.alert("Your browser doesn't support a stable version of IndexedDB.")
}
var request = indexedDB.open("kitta db1");
request.onupgradeneeded = function() {
//create Store and etc
};
request.onsuccess = function() {
db = request.result;
};
The error in iOS 8:
Type Error: null is not an Object on the line:
var request = indexedDB.open("kitta db1");
Any idea how can I fix it?
It looks like the variable indexedDB is null. The polyfill does this:
e.indexedDB=e.indexedDB||e.webkitIndexedDB||e.mozIndexedDB||e.oIndexedDB||e.msIndexedDB
So it is setting the variable to one of those values. If those values are all undefined/null, then the indexedDB variable remains null.
A simple way to test whether any of these variations have values (less Microsoft, Opera, and Mozilla) would be something like the following:
console.log('indexedDB: ', indexedDB);
console.log('webkitIndexedDB: ', webkitIndexedDB);
If webkitIndexedDB is undefined and indexedDB is undefined, then iOS apparently does not support indexedDB.
A simple search on caniuse.com says that indexedDB on iOS8 and iOS8.1 is supported but buggy.

What is the best way to detect websocket support using Javascript?

I'm trying to use Javascript to detect if a web browser supports websockets, but using only feature-based detection, I'm getting false positives, so I added a user agent test to throw out Android devices instead, which I'm not happy about. I have a Samsung Galaxy Tab 2, and here's my detection code:
var isSupported = (("WebSocket" in window && window.WebSocket != undefined) ||
("MozWebSocket" in window));
/* This line exists because my Galaxy Tab 2 would otherwise appear to have support. */
if (isSupported && navigator.userAgent.indexOf("Android") > 0)
isSupported = false;
if (isSupported)
document.write("Your browser supports websockets");
else
document.write("Your browser does not support websockets");
This code seems to work with IE, Firefox, Safari (including iPhone/iPad), and Chrome. However, the feature-based check is returning true when I use the default browser of my Samsung Galaxy Tab 2, which is incorrect because that browser does not actually support websockets. Furthermore, I don't know how many other Android devices have this same issue, so at the moment, this is the best solution I'm aware of for detection.
Is there a better way to detect websocket support other than what I'm doing? I do realize that workarounds exist for Android, such as using a different browser, which means my user agent detection code as-is would not be a good thing. My goal is to not have to rely on the user agent in the first place.
Any suggestions?
This is the shortest solution and is used by Modernizr. Simply add this to your code
supportsWebSockets = 'WebSocket' in window || 'MozWebSocket' in window;
then you can use it by running
if (supportsWebSockets) {
// run web socket code
}
I think the Modernizr library is what you are looking for: http://modernizr.com/
Once you include the library on your page, you can use a simple check like:
if(Modernizr.websockets){
// socket to me baby
}
This page comes on top in google search.
In year 2016 cutting the mustard for modern WebSockets implementation (no prefixes such as MozWebSocket) would be
if (
'WebSocket' in window && window.WebSocket.CLOSING === 2
) {
// supported
}
http://www.w3.org/TR/websockets/#the-websocket-interface
after reading #gzost's response.. I started tinkering.. since nothing else can properly detect WS's on my android phone... even websocket.org says i have it, but then fails to connect.
Anyways, try this workaround.. seems to properly detect it on/off with chrome, FF, safari and the default android browser.
var has_ws=0;
function checkWebSocket(){
try{
websocket = new WebSocket("ws:websocket.org");
websocket.close('');
}catch(e){ //throws code 15 if has socket to me babies
has_ws=1;
}
}
$(document).ready(function(){
checkWebSocket();
});
None of the above answers by itself was sufficient in my tests. The following code seems to be working fine:
function nll( o ) { return CS.undefined === typeof o || null === o; }
// ...
function check_ws_object() {
try {
var websocket = new WebSocket( "wss://echo.websocket.org" );
return true;
} catch ( e ) { ; }
return false;
}
//
function check_support() {
if ( !( WebSocket in window ) ) {
if ( nll( window.WebSocket) ) {
if ( !this.check_ws_object() ) {
alert( "This browser doesn't support HTML5 Web Sockets!" );
return false;
}
}
}
return true;
},
The above tests are sorted, so that the faster ones come first.

IE 8/9 Crashes and Freezes with a message saying "A problem with this webpage caused Internet Explorer to close and reopen the tab?"

I am using Ajax via ICallbackEventHandler in ASP.NET, what happens on the page is that a recursive call is made back to the server every 1000ms using ajax. Here is the code that makes a ajax callback every 1 second:
setTimeout("MessageServerRequest(tempLastDate);", 1000);
function MessageServerRequest(param)
{
WebForm_DoCallback('ChannelControl1','getmessage~' + param,MessageServerResponse,null,null,true);
}
function MessageServerResponse(param, context)
{
if (param.length > 0) {
var splitParam = param.split("~");
var id = splitParam[0];
var messagesHtml = splitParam[1];
var lastDate = splitParam[2];
tempLastDate = lastDate;
$('#' + id).prepend(messagesHtml);
}
setTimeout("MessageServerRequest(tempLastDate);", 1000);
}
What this does is that it gets all the latest messages from the database and returns the new messages back to the client.
Now when i removed the recursive call, IE didnt crash, but the thing is that when i use Mozilla, Safari or Opera everything works fine with the recursive call being used.
I dont understand why its working for all other browsers and not IE 8/9
System Info
Windows 7 ultimate
32 bit
visual studio 2006
IE9, even didnt work with IE8 but i upgraded to IE9
Can anyone help me with this issue please, i would be greatful?
Thanks

Categories