carrier

Dial-up in your browser: a software modem, and a phone line made of a WebRTC voice call.

瀏覽器裡的撥接:軟體做的 modem,加上一條 WebRTC 語音通話做成的電話線。

How it works 運作原理
carrier VGA

The screen is 80 columns wide: turn the phone on its side for bigger letters, or pinch to zoom.

螢幕有 80 欄寬:把手機轉成橫的字會大一些,也可以用兩指放大。

How carrier works

A phone line and a modem in your browser. The modem is software: it turns what you type into sound, and a WebRTC voice call carries the sound to the other end. Nothing goes over a DataChannel and no server relays the data. It is two modems and a phone line, and the phone line is the internet.

Making a call

Click the screen to type. You are talking to the modem, in Hayes AT commands, and it answers with OK, RING, CONNECT and the rest.

ATDT 555-1234
Dial. You hear the dial tone and the touch tones, and once the far end answers, the two modems' handshake.
ATA
Answer, when RING shows. A modem set to answer by itself (its SETUP page, or ATS0=1) picks up on the first ring.
+++
Back to command mode with the call still up. Pause a second before and after.
ATH
Hang up. Closing the tab hangs up too, and takes your phone off the network until you come back.
ATO
Back online from command mode.
ATI
Which modem you have.
ATM0, ATM2, ATL3
Speaker off, always on, loudest. It starts on until the call connects.
ATF0
Show what you type while online. It starts off (ATF1), as on a real modem: a BBS sends back what you type.

The bottom row is the terminal's status line. Ctrl-A and then a letter (or Alt and the letter) calls up the terminal's own commands: D dialing directory, H hang up, B scroll back, E local echo, O options, Z help.

The modem

Beside the screen is your modem, its lights, and its display. SETUP on the display changes the model, from the next call on: from a 300 bps Bell 103 to a 33600 bps V.34. The two modems settle on the fastest speed both have, so an old modem still gets through, slowly. It is real sound on the line: 300 bps is about 27 characters a second, 2400 about 218, 28800 with error correction about 3100, and 33600 about 3650.

A modem marked V.42 corrects errors: what a lost packet damages is sent again, and the call shows CONNECT 14400/ARQ. Without it, a lost packet is line noise, the way it was.

American boards drew their screens in CP437, the IBM PC's own characters; Taiwan's spoke Big5, two bytes to a Chinese character. Both ends have to agree: Alt-O sets the terminal's.

Running a BBS

Run a BBS turns the tab into The Carrier BBS, and the modem into a rack. Leave the tab open: anyone who dials the number on the note stuck on the rack gets in, and a call that finds a line busy rings the next. The sysop's handle is sysop; its first password comes out of the printer under the screen until you log in and change it. The board keeps its messages in this browser, so back it up now and then with the floppy drive in the rack. The switch beside the buttons puts the board on the BBS list, and so in the dialing directory on every terminal's cork board. To call your own board, use "Call it from another tab" at the bottom of the rack: each tab with a line of its own is a phone of its own.

Your own program

Run your own program puts a program of yours on the line: one JavaScript file (an ES module), chosen with "Choose a .js file". The tab becomes a station, and whoever dials the number on the note stuck on the rack is talking to your program. "Try the example" puts in a guestbook to start from. The examples' source: guestbook.js, on the station kit, and echo.js, working the modem by hand.

export default function start({ serve, storage, log }) {
    serve(async (call) => {
        call.write('Hello! What is your name? ');
        const name = await call.line({ max: 16 });
        call.write(`\nThanks for calling, ${name}. Goodbye!\n`);
    });
    log('Open for calls');
}

The default export is called once, when the station starts, with what the station gives it. serve(app, options) is the station kit: it works the modems as BBS software did (the init string, answering, CONNECT, time limits, hanging up) and calls app(call) for each call; when app returns, the line hangs up. To try it, use "Call it from another tab" at the bottom of the rack. The screen shows what the program sends the caller; the line's key on the rack shows another line, and what the program logs comes out of the printer.

What a call can do

write(text)
Send text, \n becoming CR LF; or a Uint8Array, as it is
key()
The next key: a character, '\r', '\b', 'up', 'down', 'left', 'right', 'esc'
line({ max, mask, initial, trim })
A line, echoed as typed (or as * with mask), with Backspace
choose(keys)
One of these keys, either case
yes(question, otherwise)
A yes or no; Enter gives otherwise
pause(), more(text)
"Press any key"; long text a screenful at a time
width(text), fit(text, columns)
Columns on the caller's screen, which depend on the charset
node, lines, speed, charset
The line the call is on (from 1), how many lines there are, the CONNECT speed
name
Set it once you know who is calling: the other lines see it
online(), message(node, text), tell(text)
Who is on each line; a message to another line; text for this caller from elsewhere
timeLeft()
Minutes left on this call
callSysop(), chat()
Ring the sysop at the station; if they come, chat until they press Esc
send({ name, data })
Send a file by ZMODEM; the caller's terminal starts receiving by itself
log(text)
A line on the station's printer

When the caller hangs up, goes idle too long or runs out of time, whatever is waiting on them throws a Hangup. Let it go: the kit says goodbye and frees the line.

Options, and the rest

charset
'cp437' (the default), as American boards spoke, or 'big5', as Taiwan's did; the caller's terminal has to be set the same (Alt-O)
minutes, idle
How long a call may last; minutes without a key before hanging up (10)
storage
get, set, delete, keys(prefix), all async: kept in this browser, shared by every line; the rack's floppy drive backs it up
ansi
CLEAR, RESET and colours: color.title, color.key, color.dim, color.good, color.bad
port, ports
The modems' serial ports, without the kit: bytes, DCD, RI, DSR and DTR, and your own AT commands (see echo.js)

A program runs in a worker in a sandboxed iframe: no fetch, no WebSocket, no importing other files, so bundle it into one. It has timers, TextEncoder and TextDecoder, and crypto.getRandomValues. Putting in a file of the same name again replaces the code and keeps the data.

carrier 是怎麼運作的

一條電話線、一台 modem。modem 是軟體,把打的字變成聲音,經 WebRTC 的語音通話送到對方。沒有經過 DataChannel,也沒有伺服器轉送資料,就是兩台 modem 和一條電話線,而電話線是網際網路。

打電話

點一下螢幕就能打字。打的是 Hayes 的 AT 指令,modem 會回 OK、RING、CONNECT 這些結果。

ATDT 555-1234
撥號。先聽到撥號音和按鍵音,對方接起來後是兩邊 modem 交握的聲音。
ATA
接聽。有人打來時畫面會出現 RING。在 modem 的設定頁打開自動接聽(或打 ATS0=1),響一聲就自己接。
+++
連線中回到指令模式,電話不掛。前後各停一秒。
ATH
掛斷。關掉分頁也會掛斷,這支電話也就拆了,別人打來會聽到空號。
ATO
從指令模式回到連線。
ATI
看看自己的 modem 是哪一台。
ATM0、ATM2、ATL3
喇叭關掉、一直開著、最大聲。預設只在接通前開著。
ATF0
連線時把自己打的字也顯示在自己的畫面上。預設不顯示(ATF1),像真的 modem:BBS 會把你打的字送回來。

畫面最下面一列是終端機的狀態列。先按 Ctrl-A 再按字母(或 Alt 加字母)叫出終端機自己的功能:D 撥號簿、H 掛斷、B 往回看、E 本地回顯、O 設定、Z 說明。

數據機

螢幕旁邊是你的 modem、它的燈號和顯示幕。顯示幕的「設定」可以換型號,下一通電話起生效:從 300 bps 的 Bell 103 到 33600 bps 的 V.34。兩邊交握時會談出都支援的最快速度,所以舊的 modem 也打得通,只是慢。線路上真的是聲音:300 bps 一秒大約 27 個英文字,2400 bps 大約 218 個,28800 bps 加上錯誤更正大約 3100 個,33600 bps 大約 3650 個,中文字各是一半。

標著 V.42 的 modem 有錯誤更正:網路掉封包時,壞掉的資料會重送,接通時顯示 CONNECT 14400/ARQ。沒有錯誤更正的 modem,掉封包就是當年的線路雜訊。

美國的 BBS 用 IBM PC 的 CP437 字元畫畫面,台灣的用 Big5,一個中文字兩個位元組。兩邊要一致:Alt-O 設定終端機的字集。

開站

按「開站」,這個分頁就變成 carrier BBS 中文站,modem 也變成一整個機櫃。分頁開著就是開站了:別人撥機櫃上便條寫的號碼就打得進來,第一條線忙著就接到下一條。站長的代號是 sysop,起始密碼會從螢幕下方的印表機印出來,登入後改掉。站上的資料存在這個瀏覽器裡,記得偶爾用機櫃裡的軟碟機備份。按鈕旁的開關可以把站列入 BBS 名單,大家終端機旁看板上的撥號簿裡都看得到。要打給自己的站,用機櫃最下面的「從另一個分頁打進來」:每個有自己線路的分頁就是一支電話。

自己的程式

按「自己的程式」,可以把自己寫的程式接上線:一個 JavaScript 檔(ES module),用「選擇 .js 檔」裝進來。這個分頁就變成一個站台,別人撥機櫃便條上的號碼,就是在跟你的程式說話。「試用範例」會裝上一個留言簿,可以從它改起。範例原始碼:guestbook.js(用站台工具組)、echo.js(自己操作 modem)。

export default function start({ serve, storage, log }) {
    serve(async (call) => {
        call.write('你好!你叫什麼名字?');
        const name = await call.line({ max: 16 });
        call.write(`\n${name},謝謝來電,再見。\n`);
    }, { charset: 'big5' });
    log('開站了');
}

default export 在開站時被呼叫一次,參數是站台給的東西。serve(app, options) 是站台工具組:它照當年 BBS 軟體的方式操作 modem(初始化字串、接電話、讀 CONNECT、時間限制、掛斷),每一通電話呼叫一次 app(call),app 結束就掛斷。要試打,用機櫃最下面的「從另一個分頁打進來」。螢幕上是程式送給對方的畫面,按機櫃上的「第 n 線」看別條線,程式的 log 從印表機印出來。

call 能做的事

write(text)
送字,\n 會變成 CR LF;也可以直接送 Uint8Array
key()
等下一個鍵:字元、'\r'、'\b'、'up'、'down'、'left'、'right'、'esc'
line({ max, mask, initial, trim })
讀一行,邊打邊顯示(mask 時顯示成 *),可以倒退
choose(keys)
只接受這幾個鍵之一,大小寫都可以
yes(question, otherwise)
問是或否,直接按 Enter 就是 otherwise
pause()、more(text)
「按任一鍵」;長文一頁一頁顯示
width(text)、fit(text, columns)
字在對方螢幕上佔幾格(依字集而定),或剪成幾格
node、lines、speed、charset
這通在第幾線(從 1 算)、共幾線、CONNECT 的速度、字集
name
知道對方是誰後設定,其他線看得到
online()、message(node, text)、tell(text)
各線上是誰;傳話給別條線;從別處插話給這位
timeLeft()
這通還剩幾分鐘
callSysop()、chat()
呼叫站長;站長來了就逐字聊天,直到站長按 Esc
send({ name, data })
用 ZMODEM 送檔,對方的終端機會自己開始收
log(text)
印在站台的印表機上

對方掛斷、太久沒按鍵或時間到時,正在等對方的方法會丟出 Hangup。不用接住,工具組會說再見、把線路空出來。

選項和其他 API

charset
'cp437'(預設,美國的站)或 'big5'(台灣的站);對方的終端機要設成一樣(Alt-O)
minutes、idle
每通可以多久;幾分鐘沒按鍵就掛斷(預設 10)
storage
get、set、delete、keys(prefix),都是 async:存在這個瀏覽器裡,所有線共用,機櫃的軟碟機可以備份
ansi
CLEAR、RESET 和顏色:color.title、color.key、color.dim、color.good、color.bad
port、ports
不用工具組,直接操作 modem 的序列埠:位元組、DCD、RI、DSR、DTR,自己下 AT 指令(見 echo.js)

程式在沙盒 iframe 裡的 worker 跑:沒有 fetch、沒有 WebSocket,也不能 import 別的檔案,所以要打包成一個檔。有計時器、TextEncoder、TextDecoder 和 crypto.getRandomValues。再裝一次同名的檔案會換掉程式、保留資料。